[R] Combined grouped and stacked bargraph

Jim Lemon jim at bitwrit.com.au
Wed May 2 11:20:19 CEST 2012


On 05/02/2012 04:18 PM, Nicola Van Wilgen wrote:
> Dear R-list
>
>
>
> I am having some trouble drawing a bar-graph with two groups, both of
> which are stacked.
>
>
>
> Here are my data in the dput format:
>
>
>
> cs.not.log.bp<- (
>
> structure(c(168, 69, 16, 69, 41, 6, 148, 6, 5, 4, 7, 4, 4, 2, 7, 2, 4,
> 2, 4, 2, 1, 0, 2, 0), .Dim = c(4L, 6L), .Dimnames = list(
>
>      c("IUCN.Terrestrial", "IUCN.Marine", "National.CS.Terrestrial",
>
>      "National.CS.Marine"), c("NE", "LC", "NT", "VU", "EN", "CR"
>
>      )))                 )
>
>
>
> The database format is shown below, where columns represent conservation
> status and the rows are two classification methods each split into
> marine and terrestrial:
>
>
>
>                           NE  LC NT VU EN CR
>
> IUCN.Terrestrial        168  41  5  4  4  1
>
> IUCN.Marine              69   6  4  2  2  0
>
> National.CS.Terrestrial  16 148  7  7  4  2
>
> National.CS.Marine       69   6  4  2  2  0
>
>
>
> I would like to plot the conservation status according to two
> classifications (i.e. my groups - IUCN status and national status), and
> for each of those groups I would like data for the marine and
> terrestrial species to be stacked.
>
>
>
> I have tried the following code (where cs.not.log.bp is my data), but it
> does not work:
>
>
>
> barplot(cs.not.log.bp[c(1:2),], xlab = "Conservation status", ylab =
> "Number of species", col = c("grey90","grey80"), ylim = c(0,250), space
> = 2)
>
>
>
> barplot(cs.not.log.bp[c(3:4),], col = c("grey60","grey30"), beside =
> T,add = T,names.arg = NA)
>
>
>
> legend("topright",c("IUCN Terrestrial","IUCN Marine","National CS
> Terrestrial","National CS Marine"),  col =
> c("grey90","grey80","grey60","grey30"), pch = 15)
>
>
>
> What happens is that some of the data in the second group stacks onto
> the first group and then the remainder forms a second group. I would
> like only "like" data (i.e. from the same database row) to stack within
> a group.
>
>
>
> There was one other similar post on the R-list
> (http://r.789695.n4.nabble.com/barplot-question-td3670861.html ) where
> the user had the same problem as I did, but it does not seem that this
> was resolved.
>
Hi Nicola,
This is somewhat unusual. You can do a Q&D like this:

barstack<-function(x,y,heights,width,border=par("fg"),col=NA) {
  nrect<-length(heights)
  rect(rep(x-width/2,nrect),c(0,cumsum(heights[1:(nrect-1)])),
   rep(x+width/2,nrect),cumsum(heights),border=border,col=col)
}
cs.not.log.bp <- (structure(c(168, 69, 16, 69, 41, 6, 148,
  6, 5, 4, 7, 4, 4, 2, 7, 2, 4,
  2, 4, 2, 1, 0, 2, 0), .Dim = c(4L, 6L),
  .Dimnames = list(
  c("IUCN.Terrestrial", "IUCN.Marine", "National.CS.Terrestrial",
    "National.CS.Marine"), c("NE", "LC", "NT", "VU", "EN", "CR"))))

plot(0,xlim=c(0.5,6.5),ylim=c(0,240),type="n")
for(stack in 1:6) {
  barstack(stack-0.2,0,cs.not.log.bp[1:2,stack],0.4,
   col=c("lightblue","blue"))
  barstack(stack+0.2,0,cs.not.log.bp[3:4,stack],0.4,
   col=c("lightgreen","green"))
}
legend(4,200,c("IUCN Terrestrial","IUCN Marine",
  "National Terrestrial","National Marine"),
  fill=c("lightblue","blue","lightgreen","green"))

If you only need this one illustration, that is probably the easiest 
thing to do. You can add the appropriate title, axis labels and x-axis 
tick labels to finish it off. If you need to do a lot of this, it could 
be wrapped up in a little function that would incorporate or call the 
"barstack" function.

Jim



More information about the R-help mailing list