r - 如何根据列值更改线条的颜色
问题描述
我正在尝试使用 ggplot 从数据框中绘制关于仅来自欧洲和美洲的国家的历史预期寿命的图表。我的想法是让所有欧洲国家的线条变成蓝色,美洲变成红色。
这是我的代码:
ggplot(AmericasEuropeData, aes(x = year, y = lifeExp, group = country, color = country)) +
geom_line(lwd = 1, show.legend = FALSE) +
scale_color_manual(values = country_colors) +
theme_bw() + theme(strip.text = element_text(size = rel(1.1))) +
ggtitle("Americas + Europe") +
geom_vline(xintercept=2020, linetype="dashed") +
ylab("Life Expectancy") +
xlab("Year")
显示下图:
我尝试更改color = country
为,color = continent
但它使所有线条变成灰色,而不是每个大陆的不同颜色。我该如何解决这个问题?我知道我可能搞砸了一些非常简单的事情。
AmericasEuropeData
数据框预览
解决方案
推荐阅读
- c# - 从 API 检索后,DTO 属性名称的首字母大写
- pdf-generation - reportlab python为每页添加特定的图像背景
- ajax - Laravel 7,AJAX 身份验证请求上的混合内容错误
- c# - 遍历 C# 列表以查找多个匹配的字符串并返回索引位置
- python - 如何使用循环在 tkinter 中显示多个图像?
- typegraphql - 订阅 onConnect 返回值未附加到上下文
- blazor - 在 Blazor 中插入数据
- firebase - 观察成本()
- angular - 开发时使用angular和docker
- batch-file - 使用批处理脚本,我们如何在不解压缩的情况下找到压缩/压缩文件的行数?