math math - 3 months ago 10
R Question

how to combine in ggplot line / points with special values?

I'm quite new to ggplot but I like the systematic way how you build your plots. Still, I'm struggeling to achieve desired results. I can replicate plots where you have categorical data. However, for my use I often need to fit a model to certain observations and then highlight them in a combined plot. With the usual plot function I would do:

library(splines)
set.seed(10)

x <- seq(-1,1,0.01)
y <- x^2
s <- interpSpline(x,y)
y <- y+rnorm(length(y),mean=0,sd=0.1)



plot(x,predict(s,x)$y,type="l",col="black",xlab="x",ylab="y")
points(x,y,col="red",pch=4)
points(0,0,col="blue",pch=1)
legend("top",legend=c("True Values","Model values","Special Value"),text.col=c("red","black","blue"),lty=c(NA,1,NA),pch=c(4,NA,1),col=c("red","black","blue"),cex = 0.7)


My biggest problem is how to build the data frame for ggplot which automatically then draws the legend? In this example, how would I translate this into ggplot to get a similar plot? Or is ggplot not made for this kind of plots?

Note this is just a toy example. Usually the model values are derived from a more complex model, just in case you wante to use a stat in ggplot.

Answer

The key part here is that you can map colors in aes by giving a string, which will produce a legend. In this case, there is no need to include the special value in the data.frame.

df <- data.frame(x = x, y = y, fit = predict(s, x)$y)

ggplot(df, aes(x, y)) + 
  geom_line(aes(y = fit, col = 'Model values')) +
  geom_point(aes(col = 'True values')) + 
  geom_point(aes(col = 'Special value'), x = 0, y = 0) +
  scale_color_manual(values = c('True values' = "red", 
                                'Special value' = "blue", 
                                'Model values' = "black"))

enter image description here