简体   繁体   English

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

[英]R horizontal barplot with aligned plot ontop

I am having trouble getting the spacing right on a plot on top of a horizontal barplot. 我在水平条形图上方的绘图上无法正确获取间距。 It is the same general issue as described here: http://www.r-bloggers.com/adding-lines-or-points-to-an-existing-barplot/ 这是与此处所述相同的一般问题: http : //www.r-bloggers.com/adding-lines-or-points-to-an-existing-barplot/

But I am trying to use "plot" instead of "points" or "lines". 但是我试图使用“图”代替“点”或“线”。 Is there a trick for using plot to get the spacing of the bars and the points to match? 使用plot获取小节和要匹配的点的间距是否有技巧?

点和条未对齐的示例

Code: 码:

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)

Is there a reason why you don't want to use points to add the points? 您是否有理由不想使用points来添加点? If you're willing to use points you can do it like this: 如果您愿意使用points ,可以这样做:

Create barplot and save the y-coordinates of the bars to y . 创建条形图并将条形图的y坐标保存到y You haven't provided sample data, so I'll use the built-in mtcars data frame: 您尚未提供示例数据,因此我将使用内置的mtcars数据框架:

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

Now add the points. 现在添加点。 We use y as the y values, because those are the coordinates of the midpoints of each bar: 我们使用y作为y值,因为这些是每个条形的中点的坐标:

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

在此处输入图片说明

When you use par(new=TRUE) and then call plot again, you're overlaying a new plot with a new coordinate system that in general will be different from the original coordinate system. 当您使用par(new=TRUE)然后再次调用plot时,您将使用新坐标系覆盖新的plot,该坐标系通常与原始坐标系不同。

This is what worked, based on this post suggested by eipi10: midpoints returned by barplot function do not actually line up with midpoints of bars 这是有效的,基于eipi10建议的帖子: barplot函数返回的中点实际上未与条形的中点对齐

mp<-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", ylim=c(0,length(df$DIC_mM)+2)) axis(1,line=1,col="white",col.ticks="white",col.axis="white") par(new = TRUE) plot(df$d13DIC, mp, type="p", axes = FALSE, bty = "n", xlab ="",col="deepskyblue2", lwd=5, xlim=c(-50,170), lend=2, col.lab="white", ylab="", ylim=c(0,length(df$DIC_mM)+2)) 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'," \(","\‰","\)")),3,line=-0.5,at=50,col="deepskyblue2", cex=0.75) mp <-barplot(df $ DIC_mM,col = scalegreen,xlab =“ DIC mM”,horiz = TRUE,xlim = c(0,0.7),col.axis =“ white”,col.lab =“ white”,轴= FALSE,border =“ white”,ylim = c(0,length(df $ DIC_mM)+2))axis(1,line = 1,col =“ white”,col.ticks =“ white”,col.axis =“ white”)par(new = TRUE)plot(df $ d13DIC,mp,type =“ p”,axes = FALSE,bty =“ n”,xlab =“”,col =“ deepskyblue2”,lwd = 5, xlim = c(-50,170),lend = 2,col.lab =“ white”,ylab =“”,ylim = c(0,length(df $ DIC_mM)+2))轴(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM