1

我对数据进行了分组,其中每个条形都有不同的样本量,范围从 0 样本到 >600。对于不同的数据,我希望再有 2 个相同图表的面板,如果我只是在每个条形上方写下样本大小,这将使它非常拥挤/难以阅读。

我决定制作第二个轴并将样本大小绘制为条形图上的点图。但是我无法得到它,所以点在条上对齐。我尝试调整条形的宽度以及分组条形和条形组之间的间距。并且为点图设置的间距应该与这些宽度/间距相同(参见 verts)。但它显然不是(见下面链接的照片)。有谁知道出了什么问题?有什么解决办法还是我应该继续尝试用不同的方式来传达样本量?

这是我用来绘制图形的代码的精简版本以及它现在的样子。

#From https://statisticsglobe.com/r-draw-plot-with-two-y-axes
par(mar = c(5, 4, 4, 4) + 0.3)               # Additional space for second y-axis
barplot(t(mxAe), beside=T,
    space=c(0,0.75), width=c(0.75,0.75),     # Spacing of bars
    las=2, col= c("#DDCC77", "#44AA99") , 
    ylim=c(0,100) , 
    xlim=c(0.5,45),
    main="")                                 
par(new = TRUE)                              # Add new plot
plot(x=mxAe2$place,y=mxAe2$Tot, pch = 16,
 cex= 0.5,  col = 1, axes = FALSE, 
 xlab = "", ylab = "")                       # Create second plot without axes
axis(side = 4, at = pretty(range(0,800)))    # Add second axis
abline(v=verts, col="gray30", lty=3)         # Add vertical lines along dot plot points

verts <- c(1,1.75,3,3.75,5,5.75,7,7.75,9,9.75,11,11.75, 
       13, 13.75,15,15.75,17,17.75, 19, 19.75,21,21.75,
       23,23.75,25,25.75,27,27.75,29,29.75,31,31.75,
       33,33.75,35,35.75,37,37.75,39,39.75,41,41.75,43,43.75) #Position of dots

 

在此处输入图像描述

可重现的代码:

df_mxAe <- data.frame(group1 <- c(9,0,30),group2 <- c(5,20,90))
dotx <- c(1.375,2.125,3.625,4.375,5.875,6.625)
doty <- c(200, 400, 0, 600, 50, 100)
par(mar = c(5, 4, 4, 4) + 0.3)                  # Additional space for second y-axis
barplot(t(df_mxAe), beside=T, space=c(0,1), width=c(0.75,0.75),
    las=2 ,
    col= c("#DDCC77", "#44AA99") , 
    ylim=c(0,100),
    xlim=c(0.5,6.625),
    main="")                                    # Create first plot
par(new = TRUE)                                 # Add new plot
plot(x=dotx,y=doty, pch = 18,
 cex= 0.5,  col = 1, axes = FALSE, xlim=c(0.5,6.625),
 xlab = "", ylab = "")                          # Create second plot without axes
axis(side = 4, at = pretty(range(0,800)))       # Add second axis
abline(v=dotx, col="gray30", lty=3)             # Add vertical lines along dot plot points
4

1 回答 1

0

我认为鉴于您的设置,您vents可能应该看起来更像这样:

verts <- NULL
k <- 1
for(i in 1:22){
  x <- c(.375, .375+.75)
  verts <- c(verts, k+x)
  k <- max(verts) + .375 + .75
}
verts
# [1]  1.375  2.125  3.625  4.375  5.875  6.625  8.125  8.875 10.375 11.125 12.625 13.375 14.875 15.625 17.125 17.875 19.375 20.125 21.625 22.375
# [21] 23.875 24.625 26.125 26.875 28.375 29.125 30.625 31.375 32.875 33.625 35.125 35.875 37.375 38.125 39.625 40.375 41.875 42.625 44.125 44.875
# [41] 46.375 47.125 48.625 49.375

由于第一个条形图从 1 开始,宽度为 0.75,因此您希望该线位于条形图起点和终点之间的中间,即 1.375。第二根柱线从 1.75 开始,一直到 2.5。同样,这两个数字的中间是 2.125。在第二根柱线结束于 2.5 之后,有一个 0.75 的空间,这意味着第三根柱线(第二组中的第一个)从 2.5+.75 = 3.25 开始。因此,通过第三根柱线的线应位于 3.25 + .375 = 3.625 等...

于 2022-02-03T00:31:52.583 回答