gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:28:11 25 4
gpt4 key购买 nike

我使用分面 View 绘制了 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))

以下代码返回错误:

Error in grDevices::col2rgb(colour, TRUE) : invalid colour name 'mean'

最佳答案

有几种方法可以做到这一点。我会总结数据集,然后将此总结提供给 geom_vline 。这里使用虹膜数据集

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")

关于r - 如何在方面直方图中向 geom_vline 添加图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43569732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com