简体   繁体   English

在R中的组合条形图中放置次要y轴

[英]Dropping the secondary y-axis in combined barplots in R

I am combining two bar graphs using R that have the same Y-axes so I want to drop the second one as in B: 我正在使用R组合具有相同Y轴的两个条形图,因此我想像B一样删除第二个条形图:

在此处输入图片说明

I found a way to do so using ggplot2 but just wondering if its possible within the barplot feature. 我找到了使用ggplot2的方法,但只是想知道它是否可以在barplot功能内使用。

Thanks in advance! 提前致谢!

barplot does this right out of the box. barplot开箱即用。 To illustrate, let's create the data represented by the two plots: 为了说明这一点,让我们创建由两个图表示的数据:

a <- c(2,4,6,8,9)
b <- c(5.5,6.5,7.5,8.5,9)

Here is the color vector to parallel c(a,b) : 这是平行于c(a,b)的颜色向量:

col <- unlist(mapply(rep, c("#fb0019", "#8767fc"), c(2,3, 1,4)))

In this environment, just one function call does it all: 在这种环境中,只需一个函数调用即可完成所有操作:

barplot(cbind(a, b), beside=TRUE, space=c(0.2, 1.2), names.arg=c("", ""), col=col)

数字

This method passes the two vectors of data as columns of a matrix ( via cbind(a, b) ), causing it to produce two groups of bars. 此方法将两个数据向量作为矩阵的列传递( 通过 cbind(a, b) ),从而使其产生两组条形。 The values of space give, respectively, the relative spacing between within-group bars and between the groups. space的值分别给出组内条之间和组之间的相对间距。 Finally, names.arg suppresses the automatic group labels that would be posted and col=col determines the colors of the bars from left to right. 最后, names.arg禁止显示将要张贴的自动组标签,而col=col从左到右确定条形的颜色。

To make both graphs stand closer to each other, like in your example, you need to meddle with par() a bit and I'm not sure which arguments do you need to mess with. 为了使两个图彼此靠近,就像在您的示例中一样,您需要稍微混入par(),但我不确定您需要弄乱哪些参数。

If you just need to get rid of the axis you can use axes=F inside the barplot function. 如果只需要清除轴,则可以在barplot函数中使用axes=F Just be sure to specify the y axis range so you know both graphs are using the same scale. 只需确保指定y轴范围,即可知道两个图形都使用相同的比例。

a=c(1,2,3)
b=c(2,3,4)

par(mfrow=c(1,2))

barplot(a,ylim=c(0,5))
barplot(b,ylim=c(0,5),,axes=F)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM