[R] help with barplot
Thomas Levine
thomas.levine at gmail.com
Sat May 28 20:29:23 CEST 2011
You can do pretty well without ggplot actually.
boxplot(Time~paste(Incidents,Months),data=DF,border=c('grey20','red'))
On Sat, May 28, 2011 at 2:55 AM, steven mosher <moshersteven at gmail.com> wrote:
> Thanks,
>
> ggplot is on my list of things to learn before Hadley comes here to the
> bay area
> to give a session on interactive graphics in R
>
> On Fri, May 27, 2011 at 10:29 PM, Joshua Wiley <jwiley.psych at gmail.com>wrote:
>
>> Hi Steven,
>>
>> This is not, strictly speaking, the answer to your question (hopefully
>> Tom already answered that). Rather, it is the answer to questions you
>> *might* have asked (and perhaps one of them will be one you wished you
>> had asked).
>>
>> Barplots have a low data:ink ratio...you are using an entire plot to
>> convey 8 means. A variety of alternatives exist. As a minimal first
>> step, you could just use points to show the means and skip all the
>> wasted bar space, and you might add error bars in (A). You could also
>> use boxplots to give your viewers (or just yourself) a sense of the
>> distribution along with the medians (B). Another elegant option is
>> violin plots. These are kind of like (exactly like?) mirrored density
>> plots. A measure of central tendency is not explicitly shown, but the
>> *entire* distribution and range is shown (C).
>>
>> Cheers,
>>
>> Josh
>>
>> (P.S. I hit send too soon before and sent you an offlist message with
>> PDF examples)
>>
>> ## Create your data
>> DF <- data.frame(
>> Incidents = factor(rep(c("a", "b", "d", "e"), each = 25)),
>> Months = factor(rep(1:2, each = 10)),
>> Time = rnorm(100))
>>
>> ## Load required packages
>> require(ggplot2)
>> require(Hmisc)
>>
>> ## Option A
>> ggplot(DF, aes(x = Incidents, y = Time, colour = Months)) +
>> stat_summary(fun.y = "mean", geom = "point",
>> position = position_dodge(width = .90), size = 3) +
>> stat_summary(fun.data = "mean_cl_normal", geom = "errorbar",
>> position = "dodge")
>>
>> ## Option B
>> ggplot(DF, aes(x = Incidents, y = Time, fill = Months)) +
>> geom_boxplot(position = position_dodge(width = .8))
>>
>> ## Option C
>> ggplot(DF, aes(x = Time, fill = Months)) +
>> geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
>> alpha = .2, stat = "density") +
>> facet_grid( ~ Incidents) +
>> coord_flip()
>>
>> ## Option C altered
>> ggplot(DF, aes(x = Time, fill = Months)) +
>> geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
>> alpha = .2, stat = "density") +
>> facet_grid( ~ Incidents + Months) +
>> scale_y_continuous(name = "density", breaks = NA, labels = NA) +
>> coord_flip()
>>
>> On Fri, May 27, 2011 at 3:08 PM, steven mosher <moshersteven at gmail.com>
>> wrote:
>> > Hi,
>> >
>> > I'm really struggling with barplot
>> >
>> > I have a data.frame with 3 columns. The first column represents an
>> > "incident" type
>> > The second column represents a "month"
>> > The third column represents a "time"
>> >
>> > Code for a sample data.frame
>> >
>> > incidents <- rep(c('a','b','d','e'), each =25)
>> > months <- rep(c(1,2), each =10)
>> > times <-rnorm(100)
>> >
>> > # make my sample data
>> >
>> > DF <-
>> >
>> data.frame(Incidents=as.factor(incidents),Months=as.factor(months),Time=times)
>> >
>> > # now calculate a mean for the "by" groups of incident type and month
>> >
>> > pivot <-
>> >
>> aggregate(DF$Time,by=list(Incidents=DF$Incidents,Months=DF$Month),FUN=mean,simplify=TRUE)
>> >
>> > What I want to create is a bar plot where I have groupings by incident
>> type
>> > ( a,b,d,e) and within each group
>> > I have the months in order.
>> >
>> > So group 1 would be Type "a"; month 1,2;
>> > group 2 would be Type "b"; month 1,2;
>> > group 3 would be Type "d"; month 1,2;
>> > group 4 would be Type "3"; month 1,2;
>> >
>> > I know barplot is probably the right function but I'm a bit lost on how
>> to
>> > specify groupings etc
>> >
>> > TIA
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> University of California, Los Angeles
>> http://www.joshuawiley.com/
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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