[R] Grouped bar plot

Peter Ehlers ehlers at ucalgary.ca
Wed May 18 21:38:01 CEST 2011


On 2011-05-18 11:13, jctoll wrote:
> Hi,
>
> I am trying to produce a grouped bar plot from a data.frame and I'm
> having difficulties figuring out how to do so.  My data is 500 rows by
> 4 columns and basically looks like so:
>
>> head(x)
>      V1        V2        V3        V4
> 1  XOM 0.2317915 0.1610068 1.6941637
> 2 AAPL 0.6735488 0.7433611 0.1594102
> 3   GE 1.2554160 0.9237384 1.6767711
> 4  IBM 1.6296938 0.3730387 0.5858115
> 5  CVX 0.9194169 0.4785705 0.1803601
> 6   PG 0.7768241 1.7622060 0.7640163
>   . . .
>
> I would like to produce something similar to what is found at:
> http://www.statmethods.net/graphs/bar.html          # the grouped
> barplot example
> or
> http://had.co.nz/ggplot2/geom_bar.html                # the Dodged bar
> charts example
>
> Across the X-axis, for each set(row) of 3 data points(V2, V3, V4)
> associated with a symbol(V1), I would like to create a group of 3 bars
> reflecting their values.  So the Y-axis will represent the magnitude
> of values in the columns (V2, V3, V4), and X-axis will have 500 groups
> of 3 bars, for a total of 1500 bars.  I would like the color of each
> bar to reflect the column of data it represents, and to label each
> group of 3 with the corresponding symbol in column V1.
>
> I was trying to get this to work using ggplot but the y-axis in the
> example is the count, which is not what I'm after.  Any suggestions,
> to get me started down the right path would be appreciated.  Thank
> you.

Using base barplot() and calling your 6 lines of data 'd':

   barplot(t(d[-1]), names.arg=d[,1], beside=TRUE)

Give a careful reading to the definition of the 'height' argument on
the help page.

Peter Ehlers

>
> James
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list