这里的代码可以为您提供我认为您想要使用基础 R 中的 barplot 的可靠草稿。我只是将一个系列设为负数,然后手动设置 axis 中的标签以引用原始(正)值。您必须选择如何缩放这两个系列,这样比较仍然可以提供信息。我在这里通过将高度(以厘米为单位)除以 10 来做到这一点,这会产生与多年来的范围相似的范围。
# plot the first series, but manually set the range of the y-axis to set up the
# plotting of the other series. Set axes = FALSE so you can get the y-axis
# with labels you want in a later step.
barplot(A, ylim = c(-25, 25), axes = FALSE)
# plot the second series, making whatever transformations you need as you go. Use
# add = TRUE to add it to the first plot; use names.arg to get X as labels; and
# repeat axes = FALSE so you don't get an axis here, either.
barplot(-B/10, add = TRUE, names.arg = X, axes = FALSE)
# add a line for the x-axis if you want one
abline(h = 0)
# now add a y-axis with labels that makes sense. I set lwd = 0 so you just
# get the labels, no line.
axis(2, lwd = 0, tick = FALSE, at = seq(-20,20,5),
labels = c(rev(seq(0,200,50)), seq(5,20,5)), las = 2)
# now add y-axis labels
mtext("age (years)", 2, line = 3, at = 12.5)
mtext("height (cm)", 2, line = 3, at = -12.5)
par(mai = c(0.5, 1, 0.25, 0.25)) 的结果: