我想知道是否有可能获得双面条形图(例如here),其显示在数据A 之上且在数据B 之下每个X值。
数据A 例如是一个人的年龄,数据B 是同一个人的大小。这个的问题和上面例子的主要区别:A和B显然有完全不同的单位/ ylims。
示例:
{{1}}
任何想法如何解决这个问题?我不介意水平或垂直解决方案。
非常感谢!
答案 0 :(得分:2)
这里的代码可以使用基础R中的barplot
为您提供我认为您想要的实体草稿。我只是为绘图制作一个系列否定,然后手动设置标签在axis
中引用原始(正)值。您必须选择如何缩放这两个系列,以便比较仍然提供信息。我在这里通过将高度(cm)除以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))
的结果: