-1

I fitted a model and want to do a plot of observation number in x-axis and fitted in y-axis with observed value of y in y-axis too with ggplot2, something like this. I can't use colours then I need different symbols for observed and predicted.

observed <- c(0.1,0.32,0.42,0.27,0.9,0.8)
fitted <- c(0.19,0.31,0.41,0.26,0.81,0.77)
x <- c(1,2,3,4,5,6)

So basically what I want to do is put to each x the two values in y-axis (observed and fitted) with different symbols.

Community
  • 1
  • 1
Roland
  • 334
  • 2
  • 21

2 Answers2

2

You can use simple base graphics to get two different plotting characters

plot(x, observed, pch=15)
points(x, fitted, pch=17)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
2

With ggplot, make a data.frame first:

library(tidyverse)

df <- data.frame(observed = c(0.1,0.32,0.42,0.27,0.9,0.8),
                 fitted = c(0.19,0.31,0.41,0.26,0.81,0.77),
                 x = c(1,2,3,4,5,6))

Then you can plot each set of values separately, with some extra specification to get shapes:

ggplot(df) + 
    geom_point(aes(x, observed, shape = 'observed')) + 
    geom_point(aes(x, fitted, shape = 'fitted'))

...or in an approach that will scale better, reshape to long form first with tidyr::gather:

df %>% gather(value, val, -x) %>% 
    ggplot(aes(x, val, shape = value)) + 
    geom_point()

alistaire
  • 42,459
  • 4
  • 77
  • 117