3

I created some grouped boxplots, basically for each dimension on the x axis I am showing various groups. Because my dataset is quite large, I had to precalculate the values for the boxes as ggplot did not have enough memory (I used ddply and did it in pieces).

I believe this is beter than just bar charts of the averages as it shows some of the variability.

I want 2 modifications, one was to not show the whisker lines, and I have done that by setting ymin=lower and ymax=upper.

I also wanted to add the means as well, but they show all in the center of each X category, and of course I want them each aligned with its box.

to make it easier on anyone helping, I recreated the same chart using mtcars - I tried position = "dodge" and "identity" with no change

Anyone knows how to do this? I searched and did not find a way. I am also attaching a picture of my latest chart. Code is below

data(mtcars)
data <- as.data.frame(mtcars)
data$cyl <- factor(data$cyl)
data$gear <- factor(data$gear)

summ <- ddply(data, .(cyl, gear),summarize, lower=quantile(mpg,probs=0.25,na.rm=T), middle=quantile(mpg,probs=.5,na.rm=T),upper=quantile(mpg,probs=.75,na.rm=T),avg=mean(mpg,na.rm=T))

p2 <- ggplot(summ, aes(x = cyl, lower = lower, middle = middle, upper = upper,fill=gear,ymin=lower,ymax=upper))+geom_boxplot(stat = "identity")
p2 <- p2 + geom_point(aes(x = cyl, y=avg, color=gear),color="red",position="dodge")
p2 

enter image description here

4

1 回答 1

2

The problem is that the width of the points is not the same as the width of the box plots. In that case you need to tell position_dodge what width do use. ?position_dodge gives a simple example of this using points and error bars, but the principle is the same for points and box plots. In your example, replacing position="dodge" with position=position_dodge(width=0.9) will dodge the points by the same amount as the box plots.

于 2013-03-16T15:45:00.040 回答