[R] barplot usage

Marc Schwartz MSchwartz at MedAnalytics.com
Thu Apr 14 00:30:25 CEST 2005


On Wed, 2005-04-13 at 19:05 -0300, Antonio Olinto wrote:
> Hi,
> 
> I'm trying to make a barplot with the following dataframe, with information on
> relative frequency per sediment type (ST) for some species:
> 
> Species  ST1  ST2  ST3
> SP_A     10   60    30
> ...
> 
> 
> At x-axis are (should be ...) the species names and at y-axis the frequency per
> sediment, in stacked bars.
> 
> I tried to use barplot command but with no results. Could anyone help me on this?
> 
> Thanks in advance,
> 
> Samantha

You could use something like the following (presuming that your data is
a data frame called 'df'):

  barplot(t(df[2:4]), names.arg = as.character(df$Species))

Note that the row values that you have (excluding the Species name) need
to be rotated 90 degrees as follows:

> t(df[2:4])
     1 ...
ST1 10 ...
ST2 60 ...
ST3 30 ...

In this case, each column represents the segments of each stacked bar
(or if you set 'beside = TRUE', the individual bars in a group of bars)

Then the labels below each bar in the plot come from the df$Species
column. I used as.character(df$Species) presuming that this column might
be a factor. If not, you can eliminate the use of as.character() here.

HTH,

Marc Schwartz




More information about the R-help mailing list