gpt4 book ai didi

r - 以矢量格式从 R 中导出图像,并为文本和绘图添加单独的图层?

转载 作者:行者123 更新时间:2023-12-04 01:21:44 25 4
gpt4 key购买 nike

如何以矢量格式导出 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() 添加了额外的文本(我在这里不使用 facets 的原因是在真实的情节中我为各个面板添加了显着性水平)

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()

但是,我不知道如何将文本/标签与不同图层中的实际绘图分开。有谁知道怎么做?

最佳答案

据我所知,ggplot 不允许您从同一绘图导出不同的层,因此这里有一个相当简单的解决方法。修改您的绘图代码以制作两个绘图:一个仅显示轴和数据,另一个仅显示文本/标签。在每个图中,您将每个元素都保留在那里(以便项目的位置保持不变),但使用主题编辑使您不想看到的元素透明。

以下是如何修改您的 RNA 图以获得一个仅包含数据的图和另一个仅包含文本/标签的图。

 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

只要您使用支持透明的文件格式保存这些绘图,您就会拥有绘图层和文本/标签层。

仅数据图:

enter image description here

纯文本情节:

enter image description here

关于r - 以矢量格式从 R 中导出图像,并为文本和绘图添加单独的图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53195047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com