vendredi 27 octobre 2017

ggplot in an ifelse statement prints list of data instead of plot

I'm a little confused by this behavior:

test = 2
y = data.frame(a = 1:4, b = c(1, 1, 2, 3))
p = ggplot(data = y, aes(x = a, y = b)) + 
  geom_line()

ifelse(test == 2, p + labs(caption = "XXX"), p) ## prints y in a list
p + labs(caption = "XXX")                       ## prints plot

Why does the ggplot print a list only when put inside an ifelse statement?

Aucun commentaire:

Enregistrer un commentaire