【问题标题】:R base plotting, arrange multiple plotsR基础绘图,排列多个绘图
【发布时间】: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.triangle2bottom.triangle != bottom.triangle2 那样完全重现,并且名称更改还不够,因为使用了位置参数。在空的 R 环境中测试代码。请使用library() 调用指定所有非基础包。
  • 同意@Parfait - 如果您希望有人处理您的特定代码,您需要对其进行编辑以便我们运行它。例如,您的 for 循环使用一堆参数调用 bottom.triangle(),而您在上面发布的代码定义了没有参数的函数 bottom.traingle2()。我也无法说出 x.dist 应该是什么,也无法弄清楚您要制作的 9 个图(您的 for 循环仅循环 3 件事)。另外,是否有运行代码所需的库(dplyr 除外)?

标签: r plot


【解决方案1】:

要在指定链接使用绘图解决方案,您需要调整到 iris 数据,包括您的计算列,nrm.Sepalnrm.Petal em> 在两个函数中。然后,考虑by 而不是split,将子集传递给两个函数进行绘图。但是,该图只会生成 1 X 3。不清楚 3 X 3 是如何生成的。您在上面发布的链接实际上是重复的

数据

iris$nrm.Sepal <- iris$Sepal.Width / iris$Sepal.Length
iris$nrm.Petal <- iris$Petal.Width / iris$Petal.Length

函数

top.triangle <- function(my.data) {

  plot(nrm.Sepal ~ nrm.Petal, data= my.data, axes=FALSE, ylab="", xlab="", 
       main='', xlim=c(0, 1), ylim=c(0, 1), xaxt="n", yaxt="n", asp=1)
  mtext(my.data$Species[[1]], 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(my.data) {
  x.dist <- .5
  my.data.2 <- transform(my.data, nrm.Sepal=nrm.Sepal + x.dist)

  points(nrm.Petal ~ nrm.Sepal, data=my.data.2, col="red", 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] + 3)
  x.at <- axisTicks(par()$usr[1:2], 0) + x.dist
  axis(side=1, las=1, pos=0, at=x.at, 
       labels=FALSE, xpd=TRUE)
  mtext(seq(0, 1, 0.2), 1, 0, at=x.at, cex=0.7)
  axis(4, las=1, pos=1 + x.dist)
  lines(0:1 + x.dist, 0:1, xpd=TRUE)
}

情节

par(mar=c(1, 4, 8, 6), oma=c(2, 0, 0, 2), mfrow=c(2,3))

by(iris, iris$Species, function(sub){
  top.triangle(sub)
  bottom.triangle(sub)
})

【讨论】:

  • 抱歉错别字,我编辑了帖子。 iris 数据集只是一个可行的例子。我只是把这个数字放在一起作为例子。我的这个数字的数据集有更多的类别。我对基础绘图不太熟悉,它有助于查看更多解决方案。我会阅读更多关于“by”的内容。
  • 你测试你的代码了吗?正如我在上面评论的那样,当您传入函数未定义的参数时,您不能简单地重命名函数。请dput 提供reproducible example 的实际数据样本。但是,由于此解决方案显示了针对数据的定制绘图功能。如果您有 9 个组,by 将捕获它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
相关资源
最近更新 更多