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)
sumVarVal=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:
library(ggplot2)
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90",
size=.5,
colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade")
However, I used to have an additional piece of code (at the end of the syntax) that allowed me to superimpose a horizontal bar on each of the four topics, indicating the mean percentage grade. Those values are stored in df$mean. Here is the code I was using:
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade") +
geom_errorbar(stat = "hline", width=0.6, colour = "blue", size = 1, aes(ymax=..y..,ymin=..y.., yintercept = mean))
With version 1.0.1
, this gives:
With version 2.0.0
, it now leads to an error:
Error: no stat called StatHline.
I know this may be connected to recent upgrades in ggplot2. I have seen other recent comments about it (geom_errorbar - "No stat called StatHline"). However, due to my code surrounding the use of stat="hline"
, when I tried some of these suggestions, I was not able to get my code to work either. Perhaps there is something I do not understand about my original code that is preventing me from being able to update this issue?
EDIT: I have taken into account some of the suggestions, and am currently using this code:
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade") +
geom_errorbar(stat = "summary", fun.y = "mean", width=0.6, colour = "blue", size = 1, aes(ymax=..y..,ymin=..y.., yintercept = mean))
This gives me an output that looks like this:
It seems that some of the mean blue lines are not lining up to their values, as given originally in the mean vector. For instance, for variable "D", it should have a mean value of 93.3, but the blue horizontal line seems to be displayed at a value of 90.0. The effect is even more dramatic in my real code (not this MWE). Any ideas what might be causing this discrepancy?
stat_hline
got removed in ggplot2 2.0.0, but never fear; it wasn't really necessary anyway. If you remove thestat
argument entirely, it will default toidentity
, which is fine. (summary
can work, too, if you prefer.) You need to change theaes
mapping, though, changingyintercept
toy
to account for the newstat
.All together,
produces