Plotting several groups of box plots side-by-side in R

后端 未结 1 1316
忘了有多久
忘了有多久 2021-02-05 23:59

I am trying to plot two box-plots in the same plot, each within the same category. I can generate the boxplots individually, but am stumped when I try to get them onto the same

相关标签:
1条回答
  • 2021-02-06 00:24
    boxplot(a, at = 0:2*3 + 1, xlim = c(0, 9), ylim = range(a, b), xaxt = "n")
    boxplot(b, at = 0:2*3 + 2, xaxt = "n", add = TRUE)
    axis(1, at = 0:2*3 + 1.5, labels = colnames(a), tick = TRUE)
    

    Note the ylim = range(a, b) parameter. The plot scale is determined by the first command, but if b contained values out of range of values in a (not in this case, but try to swap a and b), they would lie out of the plot. That's why in general you should specify the ylim here.

    You can also set tick = FALSE in the axis() command, I think it is nicer. If you don't like the space between the groups, use 0:2*2 instead of 0:2*3, and change the xlim appropriatelly.

    0 讨论(0)
提交回复
热议问题