Hi
I'm trying to use R studio to draw line graph.
I have problems with adding a legend to a ggplot2 figure.
I try many solutions,but still come out the same figure.
library(ggplot2)
df <- data.frame(China,India,USA,World,Year)
colors <- c("China" = "blue", "India" = "red", "USA" = "green","World" = "dodgerblue1")
ggplot(df, aes(x=Year)) +
geom_line(aes(y = China),color="blue",size=1.5) +
geom_line(aes(y = India),color = "red",size=1.5)+
geom_line(aes(y = USA),color="green",size=1.5)+
geom_line(aes(y = World),color="dodgerblue1",size=1.5)+
ylim(0,10000000000)+
labs(x="Year",y="population",color="country")+
scale_color_manual(values = colors)