Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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 如何在刻面直方图中向几何图形添加图例?_R_Ggplot2_Histogram_Facet_Vline - Fatal编程技术网

R 如何在刻面直方图中向几何图形添加图例?

R 如何在刻面直方图中向几何图形添加图例?,r,ggplot2,histogram,facet,vline,R,Ggplot2,Histogram,Facet,Vline,我用刻面视图绘制了3个直方图,并添加了均值和中位数的V线 我想添加一个图例,以指示哪个vline引用哪个统计数据 ggplot(x, aes(x=earnw)) + geom_histogram(binwidth=100, colour="black", fill="white") + facet_grid(tuyear ~ .) + geom_vline(data=a, aes(xintercept=earnw.mean), linetype="dashed", size=1, c

我用刻面视图绘制了3个直方图,并添加了均值和中位数的V线

我想添加一个图例,以指示哪个vline引用哪个统计数据

ggplot(x, aes(x=earnw)) + geom_histogram(binwidth=100, colour="black", fill="white")  +
facet_grid(tuyear ~ .)  +
 geom_vline(data=a, aes(xintercept=earnw.mean), linetype="dashed", size=1,   color="mean") +
 geom_vline(data=b, aes(xintercept=earnw.med), linetype="dashed", size=1, color="median") +
 scale_color_manual(name = "statistics", values = c("mean" <- "red", "median" <- "blue")) +
 labs(title="Histogram for Weekly Earnings of Respondents") + 
 labs(x="Weekly Earnings of Respondents", y="Count") +
 scale_x_continuous(breaks=seq(0,3000,200),lim=c(0,3000)) 
ggplot(x,aes(x=earnw))+geom_直方图(binwidth=100,color=“black”,fill=“white”)+
平面网格(tuyear~)+
几何线(数据=a,aes(xintercept=earnw.mean),线型=“虚线”,尺寸=1,颜色=“平均值”)+
几何线(数据=b,aes(xintercept=earnw.med),线型=“虚线”,尺寸=1,颜色=“中值”)+

scale\u color\u manual(name=“statistics”,values=c(“mean”有几种方法可以做到这一点。我将总结数据集,然后将此摘要提供给
geom\u vline
。这里使用iris数据集

iris.summary <- iris %>% 
  group_by(Species) %>% 
  summarise(mean.SL = mean(Sepal.Length), med.SL = median(Sepal.Length)) %>% 
  gather(key = stat, value = value, -Species)

ggplot(iris, aes(x = Sepal.Length)) + 
  geom_histogram() + 
  facet_wrap(~ Species) +
  geom_vline(data = iris.summary, aes(xintercept = value, colour = stat), linetype = "dashed")
iris.summary%
组别(种类)%>%
总结(mean.SL=平均值(萼片长度),med.SL=中值(萼片长度))%>%
聚集(键=统计,值=值,-种类)
ggplot(鸢尾,aes(x=萼片长度))+
几何直方图()
面_包装(~种)+
几何图形(数据=iris.summary,aes(xintercept=值,color=stat),线型=“虚线”)

scale\u color\u手册
要求将颜色作为值的输入
使用标签作为描述性属性