首页 > 解决方案 > 使用自定义主题将图例添加到 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 尝试),但我看不到让它们为我工作。

任何帮助表示赞赏,谢谢:)

我也在学习如何最好地在这里格式化我的问题 - 如果不正确,请见谅。

标签: rggplot2legend

解决方案


推荐阅读