【发布时间】:2020-09-01 15:43:08
【问题描述】:
我在 RStudio 中工作并尝试使用以下函数构建三角形图的 3x3 网格。我包含了一个可重现的示例,我遇到的错误是边距太大而无法绘制多个图,即使我正在减小宽度和高度。 我也尝试将这些保存为 png 并加载它们以与cowplot 排列,但图非常模糊,我不知道如何调整文本大小或线条粗细以使数字更清晰。
#Data
iris$nrm.Sepal <- iris$Sepal.Width / iris$Sepal.Length
iris$nrm.Petal <- iris$Petal.Width / iris$Petal.Length
df_list <- split(iris, (iris$Species))
top.triangle <- function() {
plot(my.y ~ my.x, data= my.data, axes=FALSE, ylab='', xlab="",
main='', xlim=c(0, 1), ylim=c(0, 1), xaxt="n", yaxt="n", asp=1)
mtext("Here could be your title", 3, 5, font=2, cex=1.3, adj=.95)
mtext("Position.2", 2, .75)
mtext("Position.1", 3, 2)
axis(side=2, las=1, pos=0)
axis(side=3, las=1, pos=1)
lines(0:1, 0:1)
}
bottom.triangle <- function() {
points(my.x ~ my.y, data=my.data.2, xpd=TRUE)
mtext("Position.2", 1, 1.5, at=mean(par()$usr[1:2]) + x.dist)
mtext("Position.1", 4, 3, padj=par()$usr[1] + 10)
x.at <- axisTicks(par()$usr[1:2], 0) + x.dist
axis(side=1, las=1, pos=0, at=x.at,
labels=F, xpd=TRUE)
mtext(seq(0, 1, .2), 1, 0, at=x.at)
axis(4, las=1, pos=1 + x.dist)
lines(0:1 + x.dist, 0:1, xpd=TRUE)
}
#loop for generating species specific plots
for(i in 1:(length(df_list))){
current.strain <- as.character(df_list[[i]]$Species[1])
#declare file for saving png
# png(paste0("~.test.triangle_", current.strain, ".png"), width=650, height=500)
plot.new()
my.data = iris
my.x.top = (iris %>% filter(Species == current.strain) )$nrm.Petal
my.y.top = (iris %>% filter(Species == current.strain) )$nrm.Sepal
my.x.bottom = (iris %>% filter(Species == current.strain) )$nrm.Petal
my.y.bottom = (iris %>% filter(Species == current.strain) )$nrm.Sepal
op <- par(mar=c(3, 2, 2, 2) + 0.1, oma=c(2, 0, 0, 2))
top.triangle(my.y.top, my.x.top, my.data)
bottom.triangle(my.y.bottom+x.dist, my.x.bottom, my.data)
par(op)
RP[[i]] <- recordPlot()
dev.off()
}
#for margins too large error
graphics.off()
par("mar")
par(mar=c(.1,.1,.1,.1))
#draw and arrange the plots
ggdraw() +
draw_plot(RP[[1]], x=0, y=0)
#Add remaining plots
#draw_plot(RP[[2]], x=.25, y=.25)
#draw_plot(RP[[3]], x=.25, y=.25)
(这是基于我从这个问题发布的答案,R base plot, combine mirrored right triangles)
【问题讨论】:
-
让你的 RStudio 应用程序中的绘图窗口更大(即不要让它只是很小的右下角)
-
我已经尝试过了,即使绘图窗口放大到屏幕的大部分,我也无法绘制第二个绘图。
-
您需要使用,例如,
par(mfrow=c(3,3))来制作一个 3x3 的绘图网格。 -
有什么问题?您收到错误消息吗?这里发布的情节有什么问题?此外,您的代码不能像
top.triangle != top.triangle2和bottom.triangle != bottom.triangle2那样完全重现,并且名称更改还不够,因为使用了位置参数。在空的 R 环境中测试代码。请使用library()调用指定所有非基础包。 -
同意@Parfait - 如果您希望有人处理您的特定代码,您需要对其进行编辑以便我们运行它。例如,您的 for 循环使用一堆参数调用 bottom.triangle(),而您在上面发布的代码定义了没有参数的函数 bottom.traingle2()。我也无法说出 x.dist 应该是什么,也无法弄清楚您要制作的 9 个图(您的 for 循环仅循环 3 件事)。另外,是否有运行代码所需的库(dplyr 除外)?