I generated this plot using ggplot2. A working example that will generate a similar graph is
set.seed(123)
prds=data.frame(Predicted=0.01*0:10,
outcome=abs(0.01*0:10-0.02+0.04*runif(11)))
ggplot(prds,aes(x = Predicted, y = as.numeric(outcome))) +
theme_bw() +
geom_abline(slope = 1, intercept = 0, color = "blue", lwd=1.3) +
geom_smooth(color = "red", se = FALSE, lwd=1.3) +
ylab("Observed") +
coord_cartesian(xlim = c(0,0.1), ylim = c(0,0.1), expand = F)
However, I couldn't find how to add the legend, and due to a tight deadline I ended by adding it manually using an image editing tool? Is there a way to add it by using ggplot2?
So geom_line will not do the trick. Here is how its done:
ggplot(prds, aes(x=Predicted)) +
ggtitle("How to do it")+
theme_bw() +
geom_smooth(aes(y=outcome, color="Model"), se=FALSE, lwd=1.3) +
geom_line(aes(y=Predicted, color="Optimum"), size=1.5) +
labs(x="Predicted", y="Observed", color=" ") +
scale_linetype_manual(name=" ",
values=c("Model"="red", "Optimum"="blue"),
labels=c("Model","Optimum")) +
theme(strip.text.x = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
legend.position=c(.85,.2))
There are two changes from the code I posted in the question. First, I replaced the geom_abline by geom_line and used it to plot the reference line by plotting Predicted against itself. Second, I added the theme definition, as explained here . I admit that I still do not understand everything regarding this theme definition. In any case, the most important detail regarding this specific question is how to set the legend.position, some trial and error may be needed.
typically, the feature you specify in the aesthetic appears in the legend. in your case should be something like
ggplot(data=your.data) + geom_line(aes(x=Predicted, y=Observed, color=model)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.