-3

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? nice plot

Yossi Levy
  • 79
  • 1
  • 5

2 Answers2

1

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.

Yossi Levy
  • 79
  • 1
  • 5
0

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)
efz
  • 425
  • 4
  • 9
  • Thank you for your answer but this is not helpful – Yossi Levy May 20 '20 at 11:32
  • Can you specify why this answer isn't helpful? Also it would be beneficial to all if you can provide a minimal working example of the required data. And because you place your blue line by means of `geom_abline()`, do you actually contain a variable within your data frame that contains the results of the optimal model? – statstew May 20 '20 at 18:08
  • You are right, let me specify and provide a working example that will produce a similar graph: – Yossi Levy May 21 '20 at 11:47
  • You are right. I edited the question and provided a working example that will produce a similar graph. In addition, adding the geom_line as you suggested results in connecting the data points and showing the un-smoothed line. Last but not least, is actually the question I asked: it produced a kind of legend which is outside of the graph. Before I asked my question I googled and found many examples that explain how to do it, that is, to put some kind of legend outside the graph. My question was how to put a customized legend **inside** the graph (in the bottom right corner, for example) – Yossi Levy May 21 '20 at 12:03