R基本图,合并镜像直角三角形 [英] R base plot, combine mirrored right triangles

查看:149
本文介绍了R基本图,合并镜像直角三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一些图形,这些图形由彼此成镜像排列的2个直角三角形组成.最终的图将具有唯一的数据集,但现在我正在绘制相同的数据.我对ggplot较为熟悉(可能被它宠坏了),但是我发现在R轴上移动轴位置要容易得多.如果有人知道如何在ggplot中复制这些直角三角形的图,我会得到这个答案!/p>

我在调整间距和布局时遇到麻烦.我对基本R绘图不太熟悉,很抱歉,如果这些是基本的.

我特别想:

  • 将三角形移近些

  • 使它可见,并使标签可见(并使用不是**main**的顶部标签)

  • 使对角线与轴齐平

  • 使相等长度的三角形的边"

    library(cowplot)
    my.data <- data.frame( my.x = c(.2,.4,.6, .1), my.y = c(.3, .5, .7, .9) )
    
       top.triangle <- function(){
      plot( my.y ~ my.x, data =  my.data, 
    axes = FALSE, ylab = 'Position.2', xlab = NA, main='Position.1',
    xlim=c(0,1), ylim=c(0,1),  xaxt="n", yaxt="n" )
      axis(side = 2, las = 1, pos=0)
      axis(side = 3, las = 1, pos=1)
      abline(coef = c(0,1))
       }
    
    bottom.triangle <- function() {
      plot( my.x ~ my.y, data = my.data , 
        axes = FALSE, xlab = 'Position.2', ylab = 'Position.1', xlim=c(0,1), ylim=c(0,1),  xaxt="n", yaxt="n" )
      axis(side = 1, las = 1, pos=0)
      axis(4, las = 1, pos=1) #flip label to right side
      abline(coef = c(0,1))}
    
    plot_grid(top.triangle, bottom.triangle, rel_widths = c(.5,.5))
    

谢谢!

解决方案

@GregorThomas 所建议的那样,最好绘制一个图.为此,需要transform个附加数据帧,该数据帧将值移动距离x.dist.

my.data <- data.frame(my.x=c(.2, .4, .6, .1), my.y=c(.3, .5, .7, .9))
x.dist <- .5
my.data.2 <- transform(my.data, my.y=my.y + x.dist)

现在,我已经对您的函数进行了实质性的修改,我建议逐行找出我使用了哪些参数.重要的是,我使用xpd=TRUE能够在绘图区域之外进行绘图.使用par,我将mar杜松子酒稍微扩大一点.我将mtextaxis结合使用以获得刻度线和标签.为了使对角线与轴齐平,我使用了lines而不是abline. bottom.triangle2现在使用points而不是plot,因为plot没有add=TRUE参数.我在top.triangle2中使用asp=1来制作等边三角形.

top.triangle2 <- 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.triangle2 <- 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)
}

我使用png获得可重复的输出.

png("myplot.png", width=650, height=500)
op <- par(mar=c(3, 4, 8, 12) + 0.1, oma=c(2, 0, 0, 2))
top.triangle2()
bottom.triangle2()
par(op)
dev.off()

结果

也许您自己想办法避免这么多的硬编码.

I am trying to build some figures comprised to 2 right triangles arranged in a mirror-image of each other. The final plots will have unique data set, but for now I am plotting the same data. I am more familiar with (maybe spoiled by) ggplot, but I've found that it's much easier to shift the axis locations in base R. If anyone knows how to replicate these right-triangle plots in ggplot I would take that answer!

I'm having trouble with adjusting the spacing and layout. I'm not as familiar with base R plotting, sorry if these are kinda of basic.

Specifically I'd like to:

  • move the triangles closer together

  • make it so the labels are visible (and use a top label that isn't **main**)

  • make the diagonal line flush with the axes

  • make the 'legs' of the triangles of equal length

    library(cowplot)
    my.data <- data.frame( my.x = c(.2,.4,.6, .1), my.y = c(.3, .5, .7, .9) )
    
       top.triangle <- function(){
      plot( my.y ~ my.x, data =  my.data, 
    axes = FALSE, ylab = 'Position.2', xlab = NA, main='Position.1',
    xlim=c(0,1), ylim=c(0,1),  xaxt="n", yaxt="n" )
      axis(side = 2, las = 1, pos=0)
      axis(side = 3, las = 1, pos=1)
      abline(coef = c(0,1))
       }
    
    bottom.triangle <- function() {
      plot( my.x ~ my.y, data = my.data , 
        axes = FALSE, xlab = 'Position.2', ylab = 'Position.1', xlim=c(0,1), ylim=c(0,1),  xaxt="n", yaxt="n" )
      axis(side = 1, las = 1, pos=0)
      axis(4, las = 1, pos=1) #flip label to right side
      abline(coef = c(0,1))}
    
    plot_grid(top.triangle, bottom.triangle, rel_widths = c(.5,.5))
    

Thanks!

解决方案

As @GregorThomas suggested, it's probably better to draw a single plot. For this, a transformed additional data frame is needed, that shifts the values by an distance x.dist.

my.data <- data.frame(my.x=c(.2, .4, .6, .1), my.y=c(.3, .5, .7, .9))
x.dist <- .5
my.data.2 <- transform(my.data, my.y=my.y + x.dist)

Now I've modified your functions substantially, I suggest to figure out line by line which arguments I've used. Importantly I use xpd=TRUE to be able to plot beyond the plot region. With par I expand the margins a little. I use mtext together with axis to get tickmarks and labels. To make diagonal line flush with the axes I used lines instead of abline. The bottom.triangle2 now uses points rather than plot, because plot has no add=TRUE argument. And I use asp=1 in top.triangle2 to make equilateral triangles.

top.triangle2 <- 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.triangle2 <- 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)
}

I use png to get reproducible output.

png("myplot.png", width=650, height=500)
op <- par(mar=c(3, 4, 8, 12) + 0.1, oma=c(2, 0, 0, 2))
top.triangle2()
bottom.triangle2()
par(op)
dev.off()

Result

Maybe you figure out on your own how to avoid that much hard coding.

这篇关于R基本图,合并镜像直角三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆