r - R:GGPLOT2:如何使用 stat_summary 在 ggplot2 的折线图中添加标签
问题描述
我有下面的代码,传说都是错误的。黑线显示为紫色,在图例中颜色标签是错误的。对于红色,它说黑色等。
ggplot(newdata,aes(x=immigrants.per.day,y=cc.percent),show.legend = TRUE) +
+ stat_summary(aes(y = cc.percent, color = "red"),fun = mean, geom = "line", label = c('CC'), show.legend = TRUE) +
+ stat_summary(aes(y = cd.percent, color = "blue"),fun = mean, geom = "line", show.legend = TRUE) +
+ stat_summary(aes(y = dc.percent, color = "green"),fun = mean, geom = "line", show.legend = TRUE) +
+ stat_summary(aes(y = dd.percent, color = "black"),fun = mean, geom = "line", show.legend = TRUE)
我确实按照下面的链接但无法解决我的问题。
解决方案
你可以做
ggplot(newdata,aes(x=immigrants.per.day,y=cc.percent),show.legend = TRUE) +
stat_summary(aes(y = cc.percent, color = "a1"),fun = mean, geom = "line", label = c('CC'), show.legend = TRUE) +
stat_summary(aes(y = cd.percent, color = "a2"),fun = mean, geom = "line", show.legend = TRUE) +
stat_summary(aes(y = dc.percent, color = "a3"),fun = mean, geom = "line", show.legend = TRUE) +
stat_summary(aes(y = dd.percent, color = "a4"),fun = mean, geom = "line", show.legend = TRUE)+
scale_colour_manual("",
breaks = c("a1", "a2", "a3","a4"),
values = c("red", "blue", "green","black"))
推荐阅读
- javascript - JavaScript 减少内部映射
- html - 如何在css中使用clip-path属性作为边框
- python - Python 中的 path = pathlib.Path().absolute() 是什么意思?
- ios - 如何在 Swift 中让 UIImageView 照片在一段时间后消失
- python - 使用python获取文本形式本地存储的html文件
- javascript - 如何向 document.createElement() 添加图标?
- node.js - 在 Mongodb 中的 Moongose 聚合管道中使用 $match 类型的字段:mongoose.Schema.ObjectId
- c# - 我缺少什么将我的 C# 代码连接到 MySQL 数据库
- python - 为什么 ModuleNotFoundError: No module named 'key',在 pip install key 成功后?
- javascript - 根据子组件的更改更新 Redux Store