r - 使用自定义主题将图例添加到 ggplot 折线图的问题
问题描述
我使用 ggplot 和 3 geom_line() 使用我的数据框的子集(subsetGED)制作了一个图表。我似乎根本无法在图表上找到图例!我想要它在右手边。
我们被要求使用多个 geomline() 来创建这个图。我通过以前的问题知道这可能不是最好的方法,但这是我需要做的。
这是我的自定义主题
CustomTheme <- theme(legend.text = element_text(face = "bold", colour = "black", family = "Helvetica"), axis.title = element_text(colour = "blue", family = "Helvetica"), axis.line = element_line(size = 0.8, colour = "grey"), axis.ticks = element_line(colour = "grey"), panel.grid.major = element_line(colour = "grey"), panel.grid.minor = element_blank(), panel.background = element_rect(fill = "white"), legend.title = element_text(colour = "black", face = "bold", family = "Helvetica"), legend.position = "right", plot.title = element_text(colour = "blue", face = "bold", family = "Helvetica", size = 9, hjust = 0.5))
这是我创建下图的绘图代码。
line1 <- ggplot() + geom_line(data = subsetGED, aes(x = year , y = primary_energy_consumption), na.rm = TRUE, colour = "blue", size = 1) + geom_line(data = subsetGED, aes(x = year , y = renewables_share_electricity), na.rm = TRUE, colour = "yellow", size =1) + geom_line(data = subsetGED, aes(x = year , y = renewables_share_energy), na.rm = TRUE, colour = "green", size = 1) + xlab("Year") + ylab("Energy Consumption (TWh)") + xlim(1960, 2019) + ggtitle("How renewable energy contributes to total energy consumption, illustrated with global electricity usage" ) + scale_y_continuous(trans='log10') + CustomTheme
打印(第 1 行)
我认为这与 aes 周围括号的位置有关吗?我也尝试过这种变体,但没有奏效——在 aes 周围添加了一个额外的括号。
line1 <- ggplot() + geom_line(data = subsetGED, (aes(x = year , y = primary_energy_consumption), na.rm = TRUE, colour = "blue", size = 1)) + geom_line(data = subsetGED, (aes(x = year , y = renewables_share_electricity), na.rm = TRUE, colour = "yellow", size =1)) + geom_line(data = subsetGED, (aes(x = year , y = renewables_share_energy), na.rm = TRUE, colour = "green", size = 1)) + xlab("Year") + ylab("Energy Consumption (TWh)") + xlim(1960, 2019) + ggtitle("How renewable energy contributes to total energy consumption, illustrated with global electricity usage" ) + scale_y_continuous(trans='log10') + CustomTheme
我也尝试过添加 scale_colour_manual
line1 <- ggplot() + geom_line(data = subsetGED, (aes(x = year , y = primary_energy_consumption), na.rm = TRUE, colour = "blue", size = 1)) + geom_line(data = subsetGED, (aes(x = year , y = renewables_share_electricity), na.rm = TRUE, colour = "yellow", size =1)) + geom_line(data = subsetGED, (aes(x = year , y = renewables_share_energy), na.rm = TRUE, colour = "green", size = 1)) + xlab("Year") + ylab("Energy Consumption (TWh)") + xlim(1960, 2019) + ggtitle("How renewable energy contributes to total energy consumption, illustrated with global electricity usage" ) + scale_y_continuous(trans='log10') + scale_colour_manual(values = c("blue" , "yellow" , "green")) + CustomTheme
我知道在我读过的网站上已经有类似的问题(因此改变了括号和 scale_manual 尝试),但我看不到让它们为我工作。
任何帮助表示赞赏,谢谢:)
我也在学习如何最好地在这里格式化我的问题 - 如果不正确,请见谅。
解决方案
推荐阅读
- pyspark - 如何在 Databricks pyspark 笔记本中包含额外的库/包?
- php - 将一个类实例分配给另一个类的静态成员变量
- c# - 防止玩家轮换在 Unity 中最后一秒发生变化
- javascript - .split() 在空格和引号处,保留引号 - 正则表达式/Javascript
- c# - Blazor HttpContext Graph API 提出挑战
- python - 如何调试 Python 脚本,通过管道输入哪些项目?
- docker - 仍然对docker感到困惑
- django - Django查询参数到数组
- indexing - 如何使用 BatchGetItem 每次为 DynamoDB 中的多个键获取 10 条记录?
- python - My search input functions works, but it only prints the last person's information in the list of dicts