从条形图中获取 ylim 以便在另一个函数中使用它

Get the ylim from a barplot in order to use it in another function

我绘制了两个条形图,其中一个是堆叠的,另一个不是。

b <- barplot(c(10,20,30,40), beside=T)
c <- barplot(t(cbind(c(1,2,3,4), c(3,4,2,1))), beside=F)

我希望堆叠条形图与第一个条形图具有相同的 ylim。感谢任何建议。

你可以在用 par("usr") 绘制第一个图后得到第一个图的 ylim 并将其用于第二个图:

# plot the first barplot
b <- barplot(c(10,20,30,40), beside=T)
# get the extreme coordinates for y axis
ylim_plot1 <- par("usr")[3:4]
# plot the second barplot with parameter ylim
c <- barplot(t(cbind(c(1,2,3,4), c(3,4,2,1))), beside=F, ylim=ylim_plot1)