【问题标题】:R: combine barplot and lineplot; line-dots and ticks on the x-axis not aligned to barplotR:结合条形图和线图; x 轴上的线点和刻度未与条形图对齐
【发布时间】:2015-10-15 00:57:24
【问题描述】:

我想在一个图中绘制行为心理学实验的两个结果:通过马图绘制的性能(以正确答案的百分比衡量)和通过折线图绘制的反应时间(以毫秒为单位)。

我已经弄清楚了所有事情,除了一件事:x 轴上的刻度和线图中的点与条形的中心不对齐。有人建议将条形图保存为对象,然后使用“at=”,它适用于 x 轴上的刻度,但不适用于线图中的点。我也无法通过使用“线条”功能使其工作,因为比例不同(条形图左侧为 0.7-1.0,折线图右侧为 400-900)。

par(mar=c(6, 4, 4, 4))
m<-barplot(c(0.87,0.83,0.79),ylim=c(0.7,1),xpd=FALSE,ylab="% correct")
axis(1,at=m,labels=c("cond. A", "cond. B", "cond. C"))
par(new=T)
plot(c(720, 800, 830), pch=15, ,ylim=c(400,900), xlab="", ylab="",      
      axes=F, type="b")
mtext("reaction time [ms]",side=4,line=2.5)
axis(4, ylim=c(400,900))

感谢您的任何帮助

【问题讨论】:

    标签: r plot bar-chart line-plot


    【解决方案1】:

    在您的第二个绘图中,将xlims 设置为等于barplot 的值。使用 par('usr') 获取当前限制(长度为 4 的向量;xmin、xmax、ymin、ymax)。然后当你画线时,你可以使用m作为x位置,它会对齐,因为你的xlims也对齐了。

    par(mar=c(6, 4, 4, 4))
    m<-barplot(c(0.87,0.83,0.79),ylim=c(0.7,1),xpd=FALSE,ylab="% correct")
    axis(1,at=m,labels=c("cond. A", "cond. B", "cond. C"))
    xlims <- par('usr')[1:2] # <-- get xlims
    par(new=T)
    plot(m, c(720, 800, 830), # <-- supply x coords
         pch=15, ,ylim=c(400,900), xlab="", ylab="",      
          axes=F, type="b", xlim=xlims) # <-- supply xlims
    mtext("reaction time [ms]",side=4,line=2.5)
    axis(4, ylim=c(400,900))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 2019-04-18
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多