Barplot有2个变量,2个y轴 [英] Barplot with 2 variables, 2 y-axis

查看:122
本文介绍了Barplot有2个变量,2个y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

  test< -data.frame(group = 1:10,var.a = rnorm (n = 10,mean = 500,sd = 20),var.b = runif(10))

我想要一个带2 y轴的barplot(一个用于var.a,一个用于var.2)。每组(x轴,1:10)应该有两个相邻的小节,一个用于var.a,一个用于var.b。

我不能使用一个y轴,因为var.a和var.b的大小不同现象

这对基本R来说可能吗?



谢谢

解决方案

使用图形包在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()不带轴:

  b arplot(t(as.matrix(test [,c(var.a.prop,var.b.prop)])),beside = TRUE,
yaxt =n,names.arg = test $ group)

然后使用原始值范围将坐标轴左右添加标签(标签参数)和比例值范围将标签放置在轴上( 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)))

轴(4,at = seq(0,max(test $ var.b.prop),length.out = 10),
labels = round(seq(0,max(test $ var。 (b),length.out = 10),2))



编辑:



c> pretty er,

  myLeftAxisLabs<  -  pretty(seq(0,max var.a),length.out = 10))
myRight AxisLabs< - 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. (0,max(c(myLeftAxisAt,myRightAxisAt)))),
beside = TRUE,yaxt =n,names.arg = test $ group,
ylim = c
$ b轴(2,at = myLeftAxisAt,labels = myLeftAxisLabs)

轴(4,at = myRightAxisAt,labels = myRightAxisLabs)


I have the following data

test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10))

I would like a barplot with 2 y axis (one for var.a, one for var.2). Each group (x axis, 1:10) should have 2 bars next to each other, one for var.a and one for var.b.

I cannot use one y-axis because of the difference morder of magnitude of var.a and var.b

Is this possible with base R?

Thank you

解决方案

To use the graphics package in R, one could create new variables as the values in var.a and var.b converted into proportions of the maximum values in the respective variable:

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")

Then draw the plot using barplot() without the axes:

barplot(t(as.matrix(test[, c("var.a.prop", "var.b.prop")])), beside = TRUE,
  yaxt = "n", names.arg = test$group)

Then add the axes on the left and the right using the original value ranges for the labels (the labels argument) and the proportional value ranges to place the labels on the axes (the at argument) (this part is not pretty, but it gets the job done):

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))

(Sorry for the lack of an image)

EDIT:

To get the axes a bit prettyer,

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)

这篇关于Barplot有2个变量,2个y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆