小编trz*_*k.p的帖子

如何在方面直方图中向 geom_vline 添加图例?

我使用分面视图绘制了 3 个直方图,并为均值和中值添加了 vlines。

在此处输入图片说明

我想添加一个图例来指示哪个 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)) 
Run Code Online (Sandbox Code Playgroud)

以下代码返回错误:

grDevices::col2rgb(colour, TRUE) 中的错误:颜色名称“mean”无效

r histogram facet ggplot2 vline

4
推荐指数
1
解决办法
1555
查看次数

标签 统计

facet ×1

ggplot2 ×1

histogram ×1

r ×1

vline ×1