I have made a graph using ggplot and 3 geom_line() using a subset of my data frame (subsetGED). I cannot seem to get a legend on my graph at all! I would like it on the right hand side.
We have been asked to used multiple geomline() to create this graph. I am aware through previous questions this might not be the best way but it's what I need to do.
Here is my custom theme
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))
Here is my code for the plot that creates the graph below.
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
print(line1)
I think it has something to do with the location of the brackets around aes? I have also tried this variation but it did not work - adding an extra bracket around 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
I have also tried adding in 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
I am aware there are already similar questions on the site which i have read (hence the changing of brackets and scale_manual attempts) but I cannot seen to get them to work for me.
Any help is appreciated, thank you :)
I'm also learning how best to format my questions on here- sorry if it is not right.