具有不同数据的R中的双面条形图
我想知道是否有可能得到一个双面条形图(例如),显示每个X值的上方数据a和下方数据B 数据A将是例如一个人的年龄,数据B是同一个人的大小。这方面的问题以及与上述示例的主要区别在于:A和B具有明显完全不同的单位/YLIM 示例:具有不同数据的R中的双面条形图,r,bar-chart,R,Bar Chart,我想知道是否有可能得到一个双面条形图(例如),显示每个X值的上方数据a和下方数据B 数据A将是例如一个人的年龄,数据B是同一个人的大小。这方面的问题以及与上述示例的主要区别在于:A和B具有明显完全不同的单位/YLIM 示例: X = c("Anna","Manuel","Laura","Jeanne") # Name of the Person A = c(12,18,22,10) # Age in years B = c(112,186,165,120) # Size in cm
X = c("Anna","Manuel","Laura","Jeanne") # Name of the Person
A = c(12,18,22,10) # Age in years
B = c(112,186,165,120) # Size in cm
有什么办法解决这个问题吗?我不介意水平或垂直解决方案
多谢各位 下面的代码可以让您使用基本R中的
条形图
获得我认为您需要的内容的可靠草稿。我只需要为绘图制作一个系列负片,然后手动设置轴
中的标签以参考原始(正)值。您必须选择如何缩放这两个系列,这样比较仍然是有用的。我在这里用厘米的高度除以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))
:
太棒了,正是我想要的!谢谢!:-)