【问题标题】:R plot, x-axis and y-axis touchingR 绘图,x 轴和 y 轴接触
【发布时间】:2012-06-08 02:43:50
【问题描述】:

我的问题涉及在 R 中为出版物制作图表。我使用了如下 plot 函数:

plot(x=data$SL, y=data$BD, xlab = "SL (mm)", ylab = "BD (mm)", pch=data$pch)

SL 范围从 51.7 到 73.7,BD 范围从 13.5 到 20.4。不幸的是,我还不能发布图片。

但是,为了摆脱我使用“axes=F”的框。现在的问题是缺乏对轴功能的控制。我用过:

axis(side=1, lwd=3, xpd=TRUE, at=c(min(data$SL):max(data$SL)))
axis(side=2, lwd=3, xpd=TRUE, at=c(min(data$BD):max(data$BD)))

问题是我无法让 y 轴和 x 轴与方框图中的同一点一起出现。如何让x轴和y轴互相接触?

【问题讨论】:

    标签: r plot axis smooth curve


    【解决方案1】:

    很可能设置 xaxs = "i" 和 yaxs = "i" 将帮助您获得所需的行为。

    plot(c(1,2,3),c(2,4,6),axes=F,xaxs = "i",yaxs="i",xlim=c(0,3),ylim=c(0,6))
    axis(side=1, lwd=3, xpd=TRUE, at=0:3)
    axis(side=2, lwd=3, xpd=TRUE, at=seq(0,6,2))
    

    【讨论】:

      【解决方案2】:

      尝试box(bty='L') 仅绘制框的左侧和底部。您也可以使用linessegmentsabline 自己绘制线条,并使用grconvertXgrconvertY 函数来查找绘制线条的位置。

      【讨论】:

        【解决方案3】:

        我建议您按照您概述的程序然后使用:

        box(which = "plot", bty = "l")

        例如:

        plot.new()
        plot.window(xlim = c(1, 18), ylim = c(2, 20))
        points(1:18, 2:19, pch = 1, col = "#FF7F24", cex = 1.2)
        lines(1:18,  2:19, col = "#FF7F24", lwd = 2)
        axis(side      = 1,
             lwd       = 0,
             lwd.ticks = 1,
             at        = 1:18,
             cex.axis = 0.9)
        title(main = "Plot",
              ylab = "Y-Axis")
        legend("top",
               legend = c("Legend"),
               col = c("#FF7F24"),
               text.col = c("#FF7F24"),
               pch    = 1,
               bty    = "n",
               cex    = 1.2)
        axis(side      = 2,
             lwd       = 0,
             lwd.ticks = 1)
        box(which = "plot", bty = "l")
        

        您应该将选项 lwd = 0lwd.ticks = 1 传递给单独的 axis() 调用,以防止轴的某些部分看起来比轴的其他部分更胖,因为有些部分会被您对 @987654326 的调用所覆盖@ 而有些则没有。

        我认为最后使用box() 的解决方案更通用,因为您可以在例如您不能或不想在您的 plot.defaultplot.window 通话中传递 bty = "l"

        【讨论】:

          猜你喜欢
          • 2018-04-08
          • 1970-01-01
          • 1970-01-01
          • 2013-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-02
          • 1970-01-01
          相关资源
          最近更新 更多