首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R基准绘图,排列多个绘图

R基准绘图,排列多个绘图
EN

Stack Overflow用户
提问于 2020-05-16 02:02:21
回答 1查看 319关注 0票数 3

我正在RStudio中工作,并试图用下面的函数创建一个3x3网格的三角形图。我已经包含了一个可重现的例子,我遇到的错误是页边距太大,无法绘制多个图,即使我正在减少宽度和高度。我也尝试过将它们保存为png格式,并将它们加载到cowplot中,但图形非常模糊,我不确定如何调整文本大小或线条粗细来使图形更清晰。

代码语言:javascript
复制
 #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 )

EN

回答 1

Stack Overflow用户

发布于 2020-05-16 05:26:31

要在指定的链接处使用绘图解决方案,您需要调整以适应虹膜数据,包括两个函数中的计算列、nrm.Sepal和nrm.Petal。然后,考虑使用by将子集传递给两个函数进行绘图,而不是使用split。但是,绘图将仅生成1 X 3。尚不清楚如何生成3 X 3。你上面张贴的链接实际上是重复的

Data

代码语言:javascript
复制
iris$nrm.Sepal <- iris$Sepal.Width / iris$Sepal.Length
iris$nrm.Petal <- iris$Petal.Width / iris$Petal.Length

函数

代码语言:javascript
复制
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)
}

Plot

代码语言:javascript
复制
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)
})

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61825604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档