[R] FW: Combined grouped and stacked bargraph

John Kane jrkrideau at inbox.com
Wed Apr 25 14:58:26 CEST 2012


Hi Nicola,

You have provided the code and data as requested the data is in a very unfriendly format.

If you would  supply the data in an easily useable format so that the readers here can work with it it would help.

Since it looks like you have a small data set the easiest thing is to use dput()  [see ?dput for information] and then just paste the output into your email.  I expect you would get faster and better answers.


John Kane
Kingston ON Canada


> -----Original Message-----
> From: nicola.vanwilgen at sanparks.org
> Sent: Wed, 25 Apr 2012 14:42:49 +0200
> To: r-help at r-project.org
> Subject: [R] FW: Combined grouped and stacked bargraph
> 
> Dear R list,
> 
> It appears that my request from yesterday got a bit garbled. I have
> tried to rephrase and retype it - please let me know if you can help me
> - I am having some trouble drawing a bar-graph with two groups, both of
> which are stacked.
> 
> 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. My data look like this (where the
> names for the columns are conservation status': NE, LC, NT, VU, EN and
> CR; and the matrix name is cs.not.log.bp):
> 
> 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 have tried the following code, but it does not work:
> 
> barplot(cs.not.log.bp[c(1:2),], xlab = "Conservation status", ylab =
> "Number of species", col = c("grey90","grey80"),names = cs.names, 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.
> 
> Please let me know if you have any suggestions.
> 
> Thanks and best wishes,
> 
> Nicola
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Nicola Van Wilgen
> Sent: 24 April 2012 01:12 PM
> To: r-help at r-project.org
> Subject: [R] Combined grouped and stacked bargraph
> 
> Dear R list,
> 
> 
> 
> I am having some trouble drawing a bar-graph with two groups, both of
> which are stacked.
> 
> 
> 
> A sample of my data (IUCN and national conservation status for marine
> and terrestrial species) look like this:
> 
> Status
> 
> IUCN Terrestrial
> 
> IUCN Marine
> 
> National CS Terrestrial
> 
> National CS Marine
> 
> NE
> 
> 168
> 
> 69
> 
> 16
> 
> 69
> 
> LC
> 
> 41
> 
> 6
> 
> 148
> 
> 6
> 
> NT
> 
> 5
> 
> 4
> 
> 7
> 
> 4
> 
> VU
> 
> 4
> 
> 2
> 
> 7
> 
> 2
> 
> EN
> 
> 4
> 
> 2
> 
> 4
> 
> 2
> 
> CR
> 
> 1
> 
> 0
> 
> 2
> 
> 0
> 
> 
> 
> I would like to plot the conservation status for two 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 needed to transpose
> the data to plot correctly, so my data in the code below look like this
> (where the names for data in the columns are NE, LC, NT, VU, EN and CR;
> and the matrix name is cs.not.log.bp):
> 
> 
> 
> 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 have tried the following code, but it does not work:
> 
> 
> 
> barplot(cs.not.log.bp[c(1:2),], xlab = "Conservation status", ylab =
> "Number of species", col = c("grey90","grey80"),
> 
>   names = cs.names, 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 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.
> 
> 
> 
> Please let me know if you have any suggestions.
> 
> 
> 
> Thanks and best wishes,
> 
> 
> 
> Nicola
>

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!



More information about the R-help mailing list