ggplot添加手动传说绘图而无需修改数据集
我试图在图块中添加手动传说而不修改数据集,因为数据集和标记平均值,中位数等的行是不同的概念。
解决了修改数据的问题的方法,例如添加手动传说>
这是一个简化的示例,真正的数据集更为复杂:
vec <-c(rep(1,100),rep(2,100),rep(3,80),rep(4,70),rep(5,60))
tbl <- as_tibble(vec)
mean_vec <- mean(vec)
cols <- c("Frequency"="grey",
"mean"="blue")
ggplot(as_tibble(tbl)) +
aes(x = value) +
geom_histogram(binwidth=1) +
geom_vline(xintercept=mean_vec,col="blue",size=1)+
theme_minimal() +
scale_color_manual(values=cols)+
scale_fill_manual(name="Test",values=cols)
为什么图中缺少传说?
I am trying to add a manual legend to a plot without modifying the dataset, because the dataset and lines that mark mean, median etc. are different concepts.
Approaches to solve the problem modifying the data exist, e.g. Adding manual legend to a ggplot
Here is a simplified example, the real dataset is more complex:
vec <-c(rep(1,100),rep(2,100),rep(3,80),rep(4,70),rep(5,60))
tbl <- as_tibble(vec)
mean_vec <- mean(vec)
cols <- c("Frequency"="grey",
"mean"="blue")
ggplot(as_tibble(tbl)) +
aes(x = value) +
geom_histogram(binwidth=1) +
geom_vline(xintercept=mean_vec,col="blue",size=1)+
theme_minimal() +
scale_color_manual(values=cols)+
scale_fill_manual(name="Test",values=cols)
Why is the legend missing in the plot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想拥有一个传奇,则必须绘制美学绘制。否则
scale_color/fill_manual
将无效:If you want to have a legend then you have to map on aesthetics. Otherwise
scale_color/fill_manual
will have no effect: