Error: no stat called StatHline

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 good plot (where the square size corresponds to the count of observations with this 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 had an additional piece of code (at the end of the syntax) that allowed me to lay a horizontal panel on each of the four topics, indicating the average percentage class. These values โ€‹โ€‹are stored in the df $ facility. Here is the code I used:

 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:

enter image description here

With version 2.0.0 this now leads to an error:

Error: There is no stat called StatHline.

I know this may be due to recent updates in ggplot2. I saw other recent comments about this ( geom_errorbar - "No stat called StatHline" ). However, due to my code related to using stat="hline" , when I tried some of these suggestions, I was not able to get my code to work. Perhaps there is something that I do not understand about my source code that is stopping me from updating this problem?

EDIT: I took 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:

My current output

It seems that some of the middle blue lines do not line up with their values, as originally indicated in the middle vector. For example, for variable "D", it should have an average of 93.3, but the blue horizontal line appears to be displayed with a value of 90.0. The effect is even more dramatic in my real code (and not in this MWE). Any ideas that might cause this inconsistency?

+2
source share
1 answer

stat_hline removed in ggplot2 2.0.0 , but is not afraid; in any case, it was not necessary. If you completely remove the stat argument, the default will be identity , which is great. ( summary may also work if you prefer.) You need to change the display of aes , though, changing yintercept to y to accommodate the new stat .

Together

 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(width=0.6, colour = "blue", size = 1, aes(ymax=..y.., ymin=..y.., y = mean)) 

produces

chart with medium bars in the right place

0
source

Source: https://habr.com/ru/post/1244120/


All Articles