Search code examples
rggplot2graphribbon

Geom-ribbon disappear after I saved it using ggsave in R


I used geom-ribbon in ggplot2 in R to generate the following graph:

enter image description here

however, after I used ggsave to save this graph, the obtained figure is enter image description here

Suddenly the ribbon disappeared. I am using the following command:

plotData<-ggplot(data = pd, aes(x = date, y = observed)) + geom_line(aes(color = "1"), size=0.95) +
        geom_line(aes(y = fitted,color="2"),size=0.95) + 
        geom_line(aes(y = forecast,color="3"), size=0.95) +
        scale_colour_manual(values=c("red", "blue","black"),labels = c("Observed", "Fitted", "Forecasted"), name="Data")+
        geom_ribbon(aes(ymin = lo95, ymax = hi95), alpha = .25)+
          #theme_bw()+
          #xlab("Time in Decades") + 
          #ylab(expression(paste ("Chl-a [mg/", m^3,"]"))) +
        scale_x_date(name = "Year", date_breaks = "2 year", labels=date_format("%Y ")) +
        scale_y_continuous(name =expression(paste ("Chl-a [mg ", m^-3,"]")))+
        theme(axis.text.x = element_text(size = 20, color="black" ,margin = margin(t = 10, r = 0, b = 0, l = 0), face="bold")) +
        theme(axis.text.y = element_text(size = 20,color="black",margin = margin(t = 0, r = 10, b = 0, l = 0), face="bold")) + 
        theme(axis.title.x = element_text(size = 35,margin = margin(t = 30, r = 0, b = 0, l = 0), face="bold")) +
        theme(axis.title.y = element_text(size = 35,margin = margin(t = 0, r = 30, b = 0, l = 0), face="bold")) + 
        #ggtitle("title")
        theme(legend.title = element_text(size = 20, face="bold"), legend.text = element_text(size =20, face="bold"))
       

plotData

ggsave(plotData, path="C:\\Users\\graphs",  file="dataaaa3.eps", device="eps", width=13, type = "cairo")
dev.off()

Thanks for your help.

PS: I am getting the followig warning:

Removed 24 rows containing missing values (geom_path).


Solution

  • You have to change the device to 'cairo_ps':

    ggsave("dataaaa3.eps", device=cairo_ps)