R ggplot2-多个图形中的注释方式
我尝试用每个图形的平均特定值标记我的图形:R ggplot2-多个图形中的注释方式,r,annotations,ggplot2,mean,R,Annotations,Ggplot2,Mean,我尝试用每个图形的平均特定值标记我的图形: ggplot(diamonds, aes(x = carat, fill=cut)) + stat_density(aes(ymax = ..density.., ymin = -..density..), geom = "ribbon", position = "identity") + facet_grid(. ~ cut) + xlim(0,2.5) + geom_text(data = NULL, x = 0.6, y
ggplot(diamonds, aes(x = carat, fill=cut)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
geom = "ribbon", position = "identity") +
facet_grid(. ~ cut) +
xlim(0,2.5) +
geom_text(data = NULL, x = 0.6, y = 0, label = mean(carat), size=5) +
coord_flip()
例如,在这里我想“公平”的图形显示为“公平”的平均值,“良好”的图形显示为“良好”的平均值,等等
另外,但这是一个额外的,如果平均值为1.0,我希望相对于x进行定位,而平均值显示在x=1.0,有许多方法可以获取标签(以及标签的位置)。这里,
dplyr
包用于总结diamonds
数据帧;也就是说,获得所需的手段。还请注意,标签的格式为小数点后两位。在下面的代码中,diamonds2数据框包含方法和标签,并用于调用geom\u text
library(ggplot2)
library(dplyr)
diamonds2 = transform(summarise(group_by(diamonds, cut), label = mean(carat)),
Label = sprintf("%.02f", label))
ggplot(diamonds, aes(x = carat, fill=cut)) +
stat_density(aes(ymax = ..density.., ymin = -..density..),
geom = "ribbon", position = "identity") +
facet_grid(. ~ cut) +
xlim(0, 2.5) +
geom_text(data = diamonds2, aes(label = Label, x = label, y = 0), size=5) +
coord_flip()
install.packages中的警告:包'dplyr'不可用(对于R版本2.15.1)
并且我无法更改R studio(在线服务器服务)的版本。请从plyr包中尝试ddply
:库(plyr);钻石2=转换(钻石,切割,汇总,标签=平均值(克拉)),标签=短跑(“%.02f”,标签))
否则请查看?聚合或。2.15.1-这已经快2年了。我已经按照你的建议做了,但是如果我建立这个图表,可能会看到geom_文本中的平均值与geom_箱线图中的平均值有所不同,我不知道如何解释它。。。箱线图框内的条是中间带。