以矢量格式从 R 中导出图像,并为文本和绘图添加单独的图层?
Export images from R in vector format with separate layers for text and plot?
如何将 R 中的图像导出为矢量格式,并为文本标签和实际绘图添加单独的图层?
我问是因为我正在准备发表一篇文章,编辑要求我
provide the highest quality, vector format, versions of [my] images
(.ai, .eps, .psd).
他们还说
Text and labelling should be in a separate layer to enable editing
during the production process.
我在 ggplot2 中创建了绘图,并设法以矢量格式导出它们(svg,因为这种格式显示不同于 eps 半透明阴影)。
使用 ggsave(“filename.svg”) 或者在一种情况下我使用 svg(“filename.svg”) 和 dev.off() 添加了额外的文本(我在这里不使用分面的原因是在真实情节中我为各个面板添加了显着性水平)
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()
但是,我不知道如何将text/labels与不同层次的实际情节分开。有人知道怎么做吗?
据我所知,ggplot 不允许您从同一个地块导出不同的层,所以这里有一个相当简单的解决方法。修改您的绘图代码以制作两个绘图:一个仅显示轴和数据,另一个仅显示 text/labels。在每个图中,您都将每个元素都保留在那里(以便项目的位置保持不变),但使用主题编辑使您不想看到的元素透明。
这里是如何修改你的 RNA 图以获得一个只有数据的图和第二个只有 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
只要您使用支持透明的文件格式保存这些绘图,您就会拥有您的绘图图层和您的 text/label 图层。
仅数据图:
纯文本情节:
如何将 R 中的图像导出为矢量格式,并为文本标签和实际绘图添加单独的图层?
我问是因为我正在准备发表一篇文章,编辑要求我
provide the highest quality, vector format, versions of [my] images (.ai, .eps, .psd).
他们还说
Text and labelling should be in a separate layer to enable editing during the production process.
我在 ggplot2 中创建了绘图,并设法以矢量格式导出它们(svg,因为这种格式显示不同于 eps 半透明阴影)。 使用 ggsave(“filename.svg”) 或者在一种情况下我使用 svg(“filename.svg”) 和 dev.off() 添加了额外的文本(我在这里不使用分面的原因是在真实情节中我为各个面板添加了显着性水平)
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()
但是,我不知道如何将text/labels与不同层次的实际情节分开。有人知道怎么做吗?
据我所知,ggplot 不允许您从同一个地块导出不同的层,所以这里有一个相当简单的解决方法。修改您的绘图代码以制作两个绘图:一个仅显示轴和数据,另一个仅显示 text/labels。在每个图中,您都将每个元素都保留在那里(以便项目的位置保持不变),但使用主题编辑使您不想看到的元素透明。
这里是如何修改你的 RNA 图以获得一个只有数据的图和第二个只有 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
只要您使用支持透明的文件格式保存这些绘图,您就会拥有您的绘图图层和您的 text/label 图层。
仅数据图:
纯文本情节: