R水平条形图,顶部对齐绘图

时间:2015-10-02 00:49:31

标签: r graphics plot bar-chart

我无法在水平条形图顶部的绘图上找到正确的间距。这是与此处描述的相同的一般问题: http://www.r-bloggers.com/adding-lines-or-points-to-an-existing-barplot/

但我试图用“情节”而不是“点”或“线”。是否有使用绘图获得条形间距和点匹配的技巧?

Misaligned points and bars example

代码:

barplot(df$DIC_mM,col=scalegreen, xlab="DIC mM", horiz=TRUE, xlim=c(0,0.7), 
         col.axis="white", col.lab="white", axes=FALSE, border="white")
axis(1,line=1,col="white",col.ticks="white",col.axis="white")
par(new = TRUE)
plot(df$d13DIC,df$Order, type="p", axes = FALSE, bty = "n", xlab ="",
      col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="")
axis(3,at = c(-50,0,50,100,150), line=1, col="deepskyblue2", col.ticks="deepskyblue2", 
      col.axis="deepskyblue2")
mtext(expression(paste(delta ^{13},'DIC'," \u0028","\u2030","\u0029")), 3, 
       line=-0.5,at=50,col="deepskyblue2", cex=0.75)

2 个答案:

答案 0 :(得分:1)

您是否有理由不想使用points添加积分?如果您愿意使用points,可以这样做:

创建条形图并将条形的y坐标保存到y。您还没有提供示例数据,因此我将使用内置的mtcars数据框:

y = barplot(mtcars$mpg[1:10], horiz=TRUE)

现在添加积分。我们使用y作为y值,因为它们是每个柱的中点的坐标:

points(sqrt(mtcars$mpg[11:20]), y, col="red", pch=16, cex=2)

enter image description here

当您使用par(new=TRUE)然后再次致电plot时,您将使用新坐标系覆盖新绘图,该坐标系通常与原始坐标系不同。

答案 1 :(得分:0)

根据eipi10:midpoints returned by barplot function do not actually line up with midpoints of bars

建议的这篇文章,这是有效的
  

mp< -barplot(df $ DIC_mM,col = scalegreen,xlab =" DIC mM",horiz = TRUE,xlim = c(0,0.7),col.axis =" white& #34;,col.lab =" white&#34 ;, axes = FALSE,border =" white",ylim = c(0,length(df $ DIC_mM)+2))   轴(1,线= 1,列="白色",col.ticks ="白色",col.axis ="白色&#34)   par(new = TRUE)   plot(df $ d13DIC,mp,type =" p&#34 ;, axes = FALSE,bty =" n",xlab ="",col =&#34 ; deepskyblue2",lwd = 5,xlim = c(-50,170),lend = 2,col.lab =" white",ylab ="",ylim = c( 0,长度(DF $ DIC_mM)+2))   轴(3,at = c(-50,0,50,100,150),line = 1,col =" deepskyblue2",col.ticks =" deepskyblue2",col.axis =&# 34; deepskyblue2&#34)   mtext(表达式(粘贴(delta ^ {13},' DIC'," \ u0028"," \ u2030"," \ u0029" )),3,line = -0.5,at = 50,col =" deepskyblue2",cex = 0.75)