Search code examples
rggplot2outputfigure

No output from ggplot when running as a script


Sorry to bother you with what is for sure a very simple mistake, but I still cannot figure it out by myself:

library(ggplot2)

X = rep(c(1:6),3)
Y = rep(c(1:6),3)
group = rep(c(1:3), each = 6)

data = data.frame(X = X, Y = Y, group = group)

ggplot(data, aes(x = X, y = Y, group = group)) + geom_point() 

I do not get any output from this when I run as a script -- no figure is created. What am I doing wrong here?


Solution

  • Try print the plot if you are calling it from another function or in Shiny:

    myplot <- ggplot(data, aes(x = X, y = Y, group = group)) + geom_point() 
    
    print(myplot)