1

It seems like there has to be a way to retain editable text when exporting graphics from R into a vector format (e.g., pdf, eps, svg), but I have not been able to locate it. My graphics are primarily created using ggplot2, and I am running R via RStudio on Windows.

#generate a plot
set.seed(1)
df <- data.frame(
  gp = factor(rep(letters[1:3], each = 10)),
  y = rnorm(30)
)
ds <- do.call(rbind, lapply(split(df, df$gp), function(d) {
  data.frame(mean = mean(d$y), sd = sd(d$y), gp = d$gp)
}))

ggplot(df, aes(gp, y)) +
  geom_point() +
  geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)

#export
ggsave("plot.eps") 
ggsave("plot.pdf")
ggsave("plot_cairo.pdf", device=cairo_pdf)
ggsave("plot.svg") 

All of these options generate a vector file with text (axis labels, etc) converted to outlines, which are no longer editable as text - which defeats a major point of the vector format, at least for my use case.

Mike Booth
  • 49
  • 5
  • See: https://stackoverflow.com/questions/17555331/how-to-preserve-text-when-saving-ggplot2-as-svg – MrFlick Nov 11 '22 at 19:30
  • Another way is to use `officer` + `svg` to export into a powerpoint slide where all components are editable. – Jon Spring Nov 11 '22 at 19:30

1 Answers1

0

Ok, so typical use cases, the svglite library will retain text - see plot 1 export below. If you put two plots together using the patchwork library, the text is converted to outlines and no longer retained as editable text.

set.seed(1)
df <- data.frame(
  gp = factor(rep(letters[1:3], each = 10)),
  y = rnorm(30)
)
ds <- do.call(rbind, lapply(split(df, df$gp), function(d) {
  data.frame(mean = mean(d$y), sd = sd(d$y), gp = d$gp)
}))

p1<-ggplot(df, aes(gp, y)) +
  geom_point() +
  geom_point(data = ds, aes(y = mean), colour = 'red', size = 3)

p2<-ggplot(df, aes(gp, y)) +
  geom_point() +
  geom_point(data = ds, aes(y = mean), colour = 'green', size = 3)

library(patchwork)

p3 <- p1|p2

ggsave(plot = p1, "p1.svg", device = svglite)


ggsave(plot = p3, "p3.svg", device = svglite)
Mike Booth
  • 49
  • 5