Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2-多个图形中的注释方式_R_Annotations_Ggplot2_Mean - Fatal编程技术网

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_箱线图中的平均值有所不同,我不知道如何解释它。。。箱线图框内的条是中间带。