我对数据进行了分组,其中每个条形都有不同的样本量,范围从 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