4
# set up data

x1=c(3,5,6,9,375,190);
x1
x2=c(2,2,3,30,46,60);
x2
data=rbind(x1,x2);
data
colnames(data)=c("Pig","Layer","Broiler","Dairy","Beef","Sheep")
rownames(data)=c("1980","2010")
data

# plot grouped bar by using barplot
barplot(data,
        beside=T, 
        ylab="Number of animal", 
        #cex.names=0.8, 
        #las=2, 
        col=c("darkblue","red")
)

# Since there are large differences in numbers, so I want to add a break between 200 to 340 as below:
data_T=t(data);
data_T
#install.packages("reshape")
library(reshape)
mdata <- melt(data_T, id=c("1980","2010"));
mdata
colnames(mdata)=c("Animal","Year",'value');
mdata
gap.barplot(mdata$value,
            gap=c(200,340),
            xlab="Animal",
            ytics=c(0,50,100,150,200,300,350,400),
            ylab="Number of animal",
            xaxlab=mdata$Animal,
            xaxt="n")
# xaxt="n" is esentiall to remove everything from x axis (e.g. a clean x axis)
# then define a axis using the following 
axis(side = 1, at = seq_along(mdata$Animal),mdata$Animal,tick = FALSE)
abline(h=seq(200,205,.001), col="white")  # hiding vertical lines
axis.break(axis=2,breakpos=202.5,style="slash") # break the left Y axis

But this is not exactly what I want because the bars (in gap.barplot) are not grouped as previous barplot figures :(Then my questions is how to make a broken y axis in grouped barplot figures?In another words, how to combine the function of barplot and gap.barplot?Thanks a lot for your kind help!

Wei Qin
  • 101
  • 1
  • 7

1 Answers1

4

Great thanks to Jim Lemon, the author of gap.barplot, he replied me via email(see below). With his code, I got exactly what I want. You may need to install and load package plotrix.

Hi Wei, The gap.barplot function doesn't do grouped bars (yet). You can probably get what you want with this:

#install.packages("plotrix") # only need to install once
library(plotrix)
newdata<-data
newdata[newdata>200]<-newdata[newdata>200]-140
barpos<-barplot(newdata,names.arg=colnames(newdata),
ylim=c(0,250),beside=TRUE,col=c("darkblue","red"),axes=FALSE)
axis(2,at=c(0,50,100,150,200,235),
labels=c(0,50,100,150,200,375))
box()
axis.break(2,210,style="gap")

Jim

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Wei Qin
  • 101
  • 1
  • 7