我正在用 ggplot2 绘制多个饼图,并成功地将标签绘制在正确的位置,如下所示:
df <- data.frame(annotation=rep(c("promoter", "intergenic", "intragene", "5prime", "3prime"), 3), value=c(69.5, 16, 10.7, 2.5, 1.3, 57.2, 18.8, 20.2, 2.1, 1.7, 50.2, 32.2, 15.3, 1.2, 1.1), treatment=rep(c("treated1", "treated2", "untreated"), c(5, 5, 5)))
library(ggplot2)
ggplot(data = df, aes(x = "", y = value, fill = annotation)) +
geom_bar(stat = "identity") +
geom_text(aes(label = value), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y") +
facet_grid(.~treatment)
Run Code Online (Sandbox Code Playgroud)
然后我想利用 ggrepel 使小切片数字不重叠:
library(ggrepel)
ggplot(data = df, aes(x = "", y = value, fill = annotation)) …Run Code Online (Sandbox Code Playgroud) 这是我的问题:我正在循环我的数据。对于循环的每次迭代,我创建了 3 个在同一页面上组织的图,并且我希望每次迭代将一页存储在同一个 pdf 文件中。
我正在使用 ggpubr 包中的 ggarrange (我希望图 3 显示在第二行并跨越 2 列)。
这是我如何进行(在一些测试数据上):
我创建测试数据框:
tt <- data.frame(group=rep(c("A", "B"), 3), value=1:6)
tt2 <- data.frame(x=1:10, y=1:10)
Run Code Online (Sandbox Code Playgroud)
我在对象中创建和存储图:
p1 <- ggplot(tt, aes(x=group, y=value, fill=group)) +
geom_dotplot(binaxis="y")
p2 <- ggplot(tt, aes(x=group, y=value, fill=group)) + geom_boxplot()
p3 <- ggplot(tt2, aes(x=x, y=y)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
这有效:
pdf("test.pdf")
ggarrange(ggarrange(p1, p2),
p3,
nrow=2,
heights=c(2, 1))
dev.off()
Run Code Online (Sandbox Code Playgroud)
这不起作用(它不会引发错误,但 pdf 文件不包含任何页面):
pdf("test.pdf")
for(i in 1:2){
ggarrange(ggarrange(p1, p2),
p3,
nrow=2,
heights=c(2, 1))
}
dev.off()
Run Code Online (Sandbox Code Playgroud)
我想也许嵌套的 ggarrange 可能是一个问题,但这也不起作用:
pdf("test.pdf")
for(i in …Run Code Online (Sandbox Code Playgroud)