0

I have a data frame as follows:

variable=c("D","D","C","C","C","A","B","B","B","B")
value=c(80,100,70,68,65,45,33,31,36,32)
Count=as.integer(c(5,10,4,5,2,7,3,5,6,2))
mean=c(93.3,93.3,68.2,68.2,68.2,45,33.4,33.4,33.4,33.4)
df=data.frame(variable=variable,value=value,Count=Count,mean=mean)

I can make a nice plot (where the size of the square corresponds to the count of observations with that particular x-value and y-value), as shown below:

ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade")

However, I now want to superimpose a horizontal bar to each of the four topics, indicating the mean percentage grade. Those values are stored in df$mean. I cannot figure out how to accomplish this. I have tried using the geom_line() function with the horizontal line option... but this seems to plot vertical lines!

ggplot(df, aes(variable, value)) + geom_point(aes(size = Count), pch=15) + guides(fill=guide_legend(title="New")) + theme(legend.text=element_text(size=rel(2.3)), legend.title=element_text(size=rel(2.3), face="plain"), legend.position="right", axis.text = element_text(size=rel(2.3)), axis.title = element_text(size = rel(2.3))) + labs(x="Topic", y = "Percentage Grade") + geom_line(stat = "hline", yintercept = df$mean)

Thank you...

  • Possible [duplicate](http://stackoverflow.com/questions/4244157/ggplot2-add-line-for-average-per-group) – aosmith Jun 19 '15 at 17:34
  • @aosmith Thanks. I actually modeled my last syntax after that post (I used a similar geom_line() that they did). However, the difference is that I have a vector of mean values, and they use a function to calculate the means. I cannot use that same function they use, because I am working with counts for each possible observation, and not each observation individually, as they did. –  Jun 19 '15 at 17:42
  • But did you try what the answer suggested, using `geom_errorbar`? You can just put `yintercept = mean` inside of `aes` instead of `yintercept = "mean"` outside to use your calculated values. – aosmith Jun 19 '15 at 18:15

1 Answers1

0

You can do this with geom_segment:

ggplot(df, aes(variable, value)) + 
    geom_point(aes(size = Count), pch=15) +
    geom_segment(aes(x=variable, y=mean-.1, 
                    xend=variable, yend=mean+.1),
                color="red", size=I(40))

enter image description here

tegancp
  • 1,204
  • 6
  • 13