Re: [R] Grouped bar plot

From: J Toll <jctoll_at_gmail.com>
Date: Wed, 18 May 2011 14:54:01 -0500

On Wed, May 18, 2011 at 2:38 PM, Peter Ehlers <ehlers_at_ucalgary.ca> wrote:
> 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

Thank you, that's what I was looking for and it gets me started in the right direction. I can now work on refining the layout. Thanks again.

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. Received on Wed 18 May 2011 - 19:57:51 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 18 May 2011 - 20:50:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive