I have created a plot of smooth lined curves of some data along with the corresponding data points with the following code in rstudio:
#begin code
library(ggplot2)
ggplot(mydata, aes(x,y)) + geom_point() + geom_smooth()
p <- ggplot() +
# blue plot
geom_point(data=mydata, aes(x,y)) +
geom_smooth(data=mydata, aes(x,y),fill="blue",
colour ="darkblue", size=1) +
# red plot
geom_point(data=mydata, aes(x,y2)) +
geom_smooth(data=mydata, aes(x,y2), fill="darkred",
colour="red", size=1)
p + xlab('Density') + ylab("Potential Energy (MeV)")
#endcode
Where data is fed in as x,y,y2 in three columns.
I can get the graphs to work just find, but I cannot figure out how to add a legend. The native legend()
function does not work and any attempt to use theme()
or scale_colour_manual()
just returns the graphs unaltered. How can I add a legend or graphic to differentiate the red and blue curves?