How can I export images in R in vector format with separate layers for text labels and the actual plot?
I am asking because I am preparing an article for publication and the editor requests that I
provide the highest quality, vector format, versions of [my] images (.ai, .eps, .psd).
They also state that
Text and labelling should be in a separate layer to enable editing during the production process.
I created my plots in ggplot2 and I managed to export them in vector format (svg since this format shows unlike eps semi-transparent shading). either with ggsave(“filename.svg”) or in one case where I added additional text with svg(“filename.svg”) and dev.off() (the reason why I do not use facets here is that in the real plot I add significance levels to individual panels)
library(ggplot2); library(cowplot); library(svglite)
data_set = structure(list(Target = c("Snodgrassella", "Snodgrassella", "ABPV",
"ABPV", "DWV", "DWV", "SBPV", "SBPV", "AmFV", "AmFV", "Gilliamella",
"Gilliamella"), Legend = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A", "B"), class = "factor"),
Estimate = c(69.6983166741774, 93.5474567104972, 12.5, 3.125,
0, 0, 6.25, 12.5, 0, 0, 90.625, 90.625), Nucleotide = structure(c(2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("RNA",
"DNA"), class = "factor")), row.names = c(7L, 8L, 9L, 10L,
15L, 16L, 21L, 22L, 23L, 24L, 31L, 32L), class = "data.frame")
RNA_data_set = subset(data_set, Nucleotide == "RNA")
DNA_data_set = subset(data_set, Nucleotide == "DNA")
RNA_plot <- ggplot(RNA_data_set, aes(x=Target, y=Estimate, fill = Legend))+
geom_bar(stat="identity", color = "black")+
xlab(NULL) +
theme(legend.position="none",
plot.margin = unit(x = c(0.25,0.25,0.25,1),"cm"))+
ylab("RNA")
DNA_plot <- ggplot(DNA_data_set, aes(x=Target, y=Estimate, fill = Legend))+
geom_bar(stat="identity", color = "black")+
xlab(NULL) +
theme(legend.position="none",
plot.margin = unit(x = c(0.25,0.25,0.25,1),"cm"))+
ylab("RNA")
svg("filename.svg")
plot_grid(RNA_plot, DNA_plot, nrow = 2)
grid.text(unit(0.03,"npc"),0.5, label = "Y-label (%)", rot = 90)
dev.off()
However, I do not know how to separate text/labels from the actual plot in different layers. Does anyone know how to do this?
ggplot won't let you export different layers from the same plot to my knowledge, so here's a fairly simple workaround. Modify your plot code to make two plots: one with only the axes and data showing, and another with only the text/labels. In each plot, you are keeping every element there (so that the placement of items remains the same), but using theme edits to make the elements you don't want to see transparent.
Here's how to modify your RNA plot to get one plot with only data and a second plot with only text/labels.
RNA_plot_data <- ggplot(RNA_data_set, aes(x=Target, y=Estimate, fill = Legend))+
geom_bar(stat="identity", color = "black")+
xlab(NULL) +
theme(legend.position = "none",
text = element_text(color = "transparent"), #transparent text
axis.text = element_text(color = "transparent"),
plot.margin = unit(x = c(0.25,0.25,0.25,1),"cm"))+
ylab("RNA")
RNA_plot_data
RNA_plot_text <- ggplot(RNA_data_set, aes(x=Target, y=Estimate, fill = Legend))+
geom_bar(stat = "identity", alpha = 0) + #make data transparent
xlab(NULL) +
theme(legend.position="none",
axis.line = element_blank(), #remove axes
axis.ticks = element_blank(),
#make all rectangles (plot and panel background) transparent
rect = element_rect(fill = "transparent", colour = NA),
plot.margin = unit(x = c(0.25,0.25,0.25,1),"cm"))+
ylab("RNA")
RNA_plot_text
As long as you save these plots using a file format that supports transparency, you'll have your plot layer and your text/label layer.
Data only plot:
text only plot: