简体   繁体   English

barplot y轴左右两侧的双面数据集

[英]barplot two-sided data sets left and right of y axis

I am trying to figure out how to make barplot where I can show two data sets. 我试图弄清楚如何制作可以显示两个数据集的条形图。 Each at one side of the y-axis. 每个在y轴的一侧。 I need the space for showing many data sets in few graphs. 我需要在几个图表中显示许多数据集的空间。 stacked or besides are other options but I would like to find out how to solve this task specially I have started to play around a little 其他选项也可以堆叠或除此以外,但是我想特别了解如何解决此任务,我已经开始尝试了一些

#creating data    
names<-LETTERS[1:9]

data1<-c(8, 6, 3, 2, 0, 1, 1, 3, 1)
data2<-c(0, -1,  0,  0,  0,  0,  0, -2, -1)#negative to show them on the 
                                           #left side of yaxis

data1<-matrix(data1,ncol=9,nrow=1,byrow=F)
dimnames(data1)<-list(1,names)

data2<-matrix(data2,ncol=9,nrow=1,byrow=F)
dimnames(data2)<-list(1,names)

par(fig=c(0.5,1,0,1)) # making space for the "left" barplot
barplot(data1,horiz=T,axes=T,las=1)
par(fig=c(0.35,0.62,0,1), new=TRUE)#adjusting the "left" barplot

#because the labels would be negative

# use of axes=F
barplot(data2,axes=F,horiz=T,axisnames=FALSE)
#creating a new axis with desired labels
axis(side=1,at=seq(-8,0,2),labels=c(8,6,4,2,0))

But I have difficulties to understand the concept behind fig=c(...) How can I ad a xaxis for my "left" barplot which has the same lenght ie running from 0:8 like the other one 但是我很难理解fig = c(...)背后的概念。我该如何为长度相同的“左” barplot广告xaxis,即从0:8开始运行

Thanks Alex 谢谢亚历克斯

As long as you know the axes before hand, this should work (adding in xlim argument). 只要您事先知道轴,它就可以工作(在xlim参数中添加)。

I also edited your earlier code a bit, as how I think you want your output to look: 我还对您之前的代码进行了一些编辑,因为我认为您希望输出看起来像这样:

par(mfrow=c(1,2))
barplot(data2,axes=F,horiz=T,axisnames=FALSE,
        xlim=c(-8,0))

#creating a new axis with desired labels
axis(side=1,at=seq(-8,0,2),labels=c(8,6,4,2,0))
barplot(data1,horiz=T,axes=T,las=1)

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

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