要使用graphics
包中的R,人们可以在var.a
和var.b
转换成最大值的比例在各可变创建新的变量的值:
test <- data.frame(group = 1:10, var.a = rnorm(n = 10, mean = 500, sd = 20),
var.b = runif(10))
funProp <- function(testCol) {
test[, testCol]/max(test[, testCol])
}
test$var.a.prop <- funProp("var.a")
test$var.b.prop <- funProp("var.b")
然后使用不带轴的barplot()
绘制绘图:
barplot(t(as.matrix(test[, c("var.a.prop", "var.b.prop")])), beside = TRUE,
yaxt = "n", names.arg = test$group)
然后使用标签的原始值范围(labels
参数)和比例值范围将左侧和右侧的轴添加到轴上(at
参数)(这部分不漂亮,但它不漂亮干得不错):
axis(2, at = seq(0, max(test$var.a.prop), length.out = 10),
labels = round(seq(0, max(test$var.a), length.out = 10)))
axis(4, at = seq(0, max(test$var.b.prop), length.out = 10),
labels = round(seq(0, max(test$var.b), length.out = 10), 2))
(对不起,没有图像的)
编辑:
要获得轴位pretty
呃,
myLeftAxisLabs <- pretty(seq(0, max(test$var.a), length.out = 10))
myRightAxisLabs <- pretty(seq(0, max(test$var.b), length.out = 10))
myLeftAxisAt <- myLeftAxisLabs/max(test$var.a)
myRightAxisAt <- myRightAxisLabs/max(test$var.b)
barplot(t(as.matrix(test[, c("var.a.prop", "var.b.prop")])),
beside = TRUE, yaxt = "n", names.arg = test$group,
ylim=c(0, max(c(myLeftAxisAt, myRightAxisAt))))
axis(2, at = myLeftAxisAt, labels = myLeftAxisLabs)
axis(4, at = myRightAxisAt, labels = myRightAxisLabs)
非常聪明的方法。非常感谢你。有什么办法让轴看起来更漂亮一些? – ECII 2012-03-25 08:32:33
@ECII,请参阅上面的编辑,以获得更漂亮的轴的建议。 – BenBarnes 2012-03-25 19:15:21