'Using second axis to display sample size for grouped data. Can't get spacing right so dot plot aligns with grouped bars in base R
I have grouped data, where every one of the bars has a different sample size, ranging from 0 samples to >600. I would like to have 2 more panels of this same graph for different data, and that would make it very crowded/hard to read if I simply wrote the sample size above each of the bars.
I decided to make a second axis and plot sample size as a dot plot over the bar chart. However I can't get it so the dots align over the bars. I've tried adjusting the width of the bars and spacing in between grouped bars and the sets of bars. And the spacing set for the dot plot should be the same as these widths/spaces (see verts). But its evidently not (see photo linked below). Does anyone have an idea of what is going wrong? Is there any fix or should I move on to trying a different way to communicate the sample sizes?
Here is a pared-down version of the code I am using to draw the figure and a picture of what it looks like right now.
#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
Reproducible code:
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
Solution 1:[1]
I think that given your setup, your vents should maybe look more like this:
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
Since the first bar starts at 1 and has a width of .75, you want the line to be half-way between the start and end of the bar, which would be 1.375. The second bar starts at 1.75 and goes to 2.5. Again, half-way between those two numbers is 2.125. After the second bar ends at 2.5 there is a .75 space, which means the third bar (first in the second group) starts at 2.5+.75 = 3.25. So, the line through the third bar should be at 3.25 + .375 = 3.625, etc...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DaveArmstrong |

