barplot in ggplot2

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

barplot in ggplot2

Joshua Wiley-2
Hello,

I am trying to make a barplot of means where the difference between
the barheights is a different color.  My goal is to convey the same
information as a dodged barplot, but with only four bars.  Below is
the data I am working with, and the two plots I have tried.  The
stacked plot would be great, if they were stacked in front of each
other.  Any ideas on how to do this would be appreciated.

Thanks,

Josh

##############################
plot.means <- structure(list(means = c(2.35, 1.8, 2.26086956521739,
1.90909090909091, 1.88333333333333, 1.73846153846154,
1.93333333333333, 1.75), hilow = c(0, 1, 0, 1, 0, 1, 0, 1), index =
c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L)), row.names = c(NA, -8L), .Names =
c("means", "hilow", "index"), class = "data.frame")

##Uncomment below if ggplot2 is not already loaded
#library(ggplot2)
ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) +
geom_bar(position="dodge", stat="identity") #dodged plot
ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) +
geom_bar(stat="identity") #stacked plot

##############################


--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: barplot in ggplot2

Thierry Onkelinx
Dear Joshua,

You can accomplish that by setting the position argument in geom_bar to position_dodge(width = 0).

ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) + geom_bar(position = position_dodge(width = 0.1), stat="identity")

But such a plot can be very misleading. Therefore I would suggest to use at least width = 0.1. This way the reader can see that the bars in the background starts at 0.

HTH,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
[hidden email]
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
 

> -----Oorspronkelijk bericht-----
> Van: [hidden email]
> [mailto:[hidden email]] Namens Joshua Wiley
> Verzonden: dinsdag 11 mei 2010 16:42
> Aan: [hidden email]
> Onderwerp: [R] barplot in ggplot2
>
> Hello,
>
> I am trying to make a barplot of means where the difference between
> the barheights is a different color.  My goal is to convey the same
> information as a dodged barplot, but with only four bars.  Below is
> the data I am working with, and the two plots I have tried.  The
> stacked plot would be great, if they were stacked in front of each
> other.  Any ideas on how to do this would be appreciated.
>
> Thanks,
>
> Josh
>
> ##############################
> plot.means <- structure(list(means = c(2.35, 1.8, 2.26086956521739,
> 1.90909090909091, 1.88333333333333, 1.73846153846154,
> 1.93333333333333, 1.75), hilow = c(0, 1, 0, 1, 0, 1, 0, 1), index =
> c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L)), row.names = c(NA, -8L), .Names =
> c("means", "hilow", "index"), class = "data.frame")
>
> ##Uncomment below if ggplot2 is not already loaded
> #library(ggplot2)
> ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) +
> geom_bar(position="dodge", stat="identity") #dodged plot
> ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) +
> geom_bar(stat="identity") #stacked plot
>
> ##############################
>
>
> --
> Joshua Wiley
> Senior in Psychology
> University of California, Riverside
> http://www.joshuawiley.com/
>
> ______________________________________________
> [hidden email] 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.
>

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: barplot in ggplot2

Joshua Wiley-2
Dear Thierry,

Thank you, that worked excellently, and good point about being
potentially misleading.  That had not occurred to me.

Thanks again,

Josh

On Tue, May 11, 2010 at 8:05 AM, ONKELINX, Thierry
<[hidden email]> wrote:

> Dear Joshua,
>
> You can accomplish that by setting the position argument in geom_bar to position_dodge(width = 0).
>
> ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) + geom_bar(position = position_dodge(width = 0.1), stat="identity")
>
> But such a plot can be very misleading. Therefore I would suggest to use at least width = 0.1. This way the reader can see that the bars in the background starts at 0.
>
> HTH,
>
> Thierry
>
> ----------------------------------------------------------------------------
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek
> team Biometrie & Kwaliteitszorg
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
>
> Research Institute for Nature and Forest
> team Biometrics & Quality Assurance
> Gaverstraat 4
> 9500 Geraardsbergen
> Belgium
>
> tel. + 32 54/436 185
> [hidden email]
> www.inbo.be
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
>
>> -----Oorspronkelijk bericht-----
>> Van: [hidden email]
>> [mailto:[hidden email]] Namens Joshua Wiley
>> Verzonden: dinsdag 11 mei 2010 16:42
>> Aan: [hidden email]
>> Onderwerp: [R] barplot in ggplot2
>>
>> Hello,
>>
>> I am trying to make a barplot of means where the difference between
>> the barheights is a different color.  My goal is to convey the same
>> information as a dodged barplot, but with only four bars.  Below is
>> the data I am working with, and the two plots I have tried.  The
>> stacked plot would be great, if they were stacked in front of each
>> other.  Any ideas on how to do this would be appreciated.
>>
>> Thanks,
>>
>> Josh
>>
>> ##############################
>> plot.means <- structure(list(means = c(2.35, 1.8, 2.26086956521739,
>> 1.90909090909091, 1.88333333333333, 1.73846153846154,
>> 1.93333333333333, 1.75), hilow = c(0, 1, 0, 1, 0, 1, 0, 1), index =
>> c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L)), row.names = c(NA, -8L), .Names =
>> c("means", "hilow", "index"), class = "data.frame")
>>
>> ##Uncomment below if ggplot2 is not already loaded
>> #library(ggplot2)
>> ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) +
>> geom_bar(position="dodge", stat="identity") #dodged plot
>> ggplot(plot.means, aes(x=index, y=means, fill=factor(hilow))) +
>> geom_bar(stat="identity") #stacked plot
>>
>> ##############################
>>
>>
>> --
>> Joshua Wiley
>> Senior in Psychology
>> University of California, Riverside
>> http://www.joshuawiley.com/
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> Druk dit bericht a.u.b. niet onnodig af.
> Please do not print this message unnecessarily.
>
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
> en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
> door een geldig ondertekend document. The views expressed in  this message
> and any annex are purely those of the writer and may not be regarded as stating
> an official position of INBO, as long as the message is not confirmed by a duly
> signed document.
>



--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/

______________________________________________
[hidden email] 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.