0

I create a plot from data in R using ggplot2. When saving the ggplot object to a png file, the plot looks exactly as it should (see picture). When saving the same ggplot object to a pdf, all text corrupts. Anyone knows a solution?

Results of png/pdf

 data <- data.frame(matrix(nrow=50, ncol=3)) # Generate sample data
 colnames(data) <- c("LUT", "Climate", "parameter") 
 data$LUT <- c("EM", "EP", "IM", "OF", "CF")
 data$Climate <- rep(c("amb", "fut"), each=5)
 for(i in 1:25){
 data$parameter[data$Climate=="amb"][i] <- sample(10:110,1)
 data$parameter[data$Climate=="fut"][i] <- sample(0:100,1)
 }

 data
 
 
 test_figure <- ggplot(aes(y=parameter, x=LUT, color=Climate), data=data)+ # Generate ggplot object
 geom_point()+
 scale_colour_manual(values=c("amb" = "#007089", "fut" = "#60c3d9"))+
 stat_summary(
 geom = "point",
 fun.y = "mean",
size = 5,
shape = 17)+
stat_summary(fun.data = mean_se, geom = "errorbar")+ # add errorbar
ylab("test_lab")+
theme(text = element_text(size = 22),
plot.title = element_text(size=25),


axis.text.y = element_text(size=18),
axis.text.x = element_text(size=18),
axis.title.y=element_text(size=17))


ggsave("test.figure.png", test_figure) # Save as png / pdf
pdf("test_figure.pdf")
print(test_figure)
dev.off()

Peter
  • 11,500
  • 5
  • 21
  • 31
  • Have you tried with `ggsave("test_figure.pdf", test_figure)` . This works fine in my environment. – Peter Dec 20 '22 at 18:19
  • I did try this already, unfortunately it leads to the same outcome. – F_S_aus_G Dec 20 '22 at 21:38
  • Is this a [mre]? I can't tell because I don't know r. Related: ["_How to make a great R reproducible example_"](https://stackoverflow.com/q/5963269/11107541). – starball Dec 22 '22 at 02:08

0 Answers0