3

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. 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 i.e running from 0:8 like the other one

Thanks Alex

alex
  • 31
  • 3

1 Answers1

2

As long as you know the axes before hand, this should work (adding in xlim argument).

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)
so13eit
  • 942
  • 3
  • 11
  • 22
  • thanks. nice approach but It does not look exactly like what I have in mind. The center is still (y axis) shifted to the right barplot(data1) – alex Aug 09 '13 at 21:20
  • using your approach and some testing on my own par(fig=c(0.41,0.91,0,1),new=T) barplot(data1,horiz=T,axes=T,las=1) ...Looks like what I would like to have but I still have to fix the length of the y.axis by hand – alex Aug 09 '13 at 21:24