nchimato
nchimato

Reputation: 463

Plotting intervals with barplot function R

I am using the solution in this question to try to plot a bar plot with specified intervals:

Create categorical variable in R based on range

I created my intervals and tried to use them in the barplot function but I am obviously missing a step somewhere and I'm not sure how to get it to work. Here is my code and the error I am getting:

> library(lattice)

> a = c(0,10)
> b = c(11,20)
> c = c(21,30)
> d = c(31,40)
> e = c(41,50)
> f = c(51,60)
> g = c(61,70)
> h = c(71,80)
> i = c(81,90)
> j = c(91,100)
> k = c(101,120)
> l = c(121,150)
> m = c(151,200)
> n = c(201,500)
> o = c(501,3600)

> mybins = matrix(rbind(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o), ncol=2)
> shx <- shingle(data5$q3totalScleralLensesFit, intervals=mybins)
> shx
  Intervals:
  min  max count
  1    0   10   140
  2   11   20   117
  3   21   30    78
  4   31   40    31
  5   41   50    72
  6   51   60     5
  7   61   70     6
  8   71   80    28
  9   81   90     3
  10  91  100    49
  11 101  120     7
  12 121  150    28
  13 151  200    25
  14 201  500    61
  15 501 3600    28

> bp <- barplot(shx, main="", xlab="", ylim=c(0,160), ylab="", las=2, cex.names=0.75)

Error in barplot.default(shx, main = "", xlab = "", ylim = c(0, 160),  : 
'height' must be a vector or a matrix

I don't know how to fix the error. Is there an easier way to make bins like this for a barplot or does someone have any suggestions on how to make the intervals work with barplot?

Thank you!

Upvotes: 3

Views: 4199

Answers (1)

eipi10
eipi10

Reputation: 93811

I haven't used the shingle function before, but it appears to be set up for creating a shingle plot, rather than a bar plot, although there may be a way that I'm not aware of to create a bar plot with a shingle object. In any case, the code below shows how to create a bar plot, using either base graphics, lattice, or ggplot2, but using the cut function to create the bins.

An issue to be aware of in terms of the error you received: barplot is a base graphics function. It's expecting a numeric vector of bar height values as its first argument. But shx is a shingle object, rather than a vector of heights, hence the error. In principle, someone could write a "shingle method" for barplot that would make barplot return a bar plot from a shingle object, but such a method doesn't at present exist and isn't necessary since there are other "standard" ways to create a bar plot.

As shown below, the way to plot a shingle object is by just calling the generic plot function, because plot "knows" that when it receives a shingle object, it should return a lattice shingle plot. If you run methods(plot) you'll see that plot has dozens of "methods" (including plot.shingle) that determine what plot does, depending on what type of object is fed to the plot function.

## Fake data
set.seed(5)
data5 = data.frame(q3totalScleralLensesFit = runif(1000,0,3600))

## Create shingle object

# Easier way to create bins
bins = c(0, seq(11,101,10),121,151,201,501,3601)
mybins = cbind(bins, lead(bins) - 1)[-length(bins),]

shx <- shingle(data5$q3totalScleralLensesFit, intervals=mybins)

Lattice shingle plot

plot(shx)

enter image description here

Now let's set up the data for creating a bar plot. We'll use the cut function:

# Create bins using cut function
data5$breaks = cut(data5$q3totalScleralLensesFit, 
                   breaks=c(seq(0,100,10),120,150,200,500,3600),
                   include.lowest=TRUE)

Base graphics bar plot

barplot(table(data5$breaks), horiz=TRUE, las=1)

enter image description here

lattice bar plot

barchart(data5$breaks)

enter image description here

ggplot2 bar plot

library(ggplot2)

ggplot(data5, aes(breaks)) +
  geom_bar() + 
  coord_flip() +
  theme_bw()

enter image description here

Upvotes: 5

Related Questions