[R] Using ggplot2 to plot percentages in bar chart.

Shawn Way SWay at meco.com
Tue Dec 6 14:20:30 CET 2016


This worked well!  Thank you very much.

For the record, the stat_bin was used in a solution I found on stackexchange for something similar, but I was having issues adapting.

Thanks again to all who responded.  I appreciate it greatly.

Shawn Way, PE

-----Original Message-----
From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.ca.us] 
Sent: Tuesday, December 06, 2016 12:13 AM
To: Shawn Way <SWay at meco.com>
Cc: John Kane <jrkrideau at yahoo.ca>; r-help at r-project.org
Subject: Re: [R] Using ggplot2 to plot percentages in bar chart.

I don't understand how your labelling technique was supposed to work. 
Below is my reproducible example of how I would approach this problem.
When you post next time, try to follow this method of making something we can put into our fresh R environment and see your problem directly.

---
library(dplyr)
library(tidyr)
library(ggplot2)

dta <- read.table( text=
"  MASTERPAK2LT MASTERPAK4LT MASTERPAK7LT MASTERPAK10LT MASTERPAK22LT
1            X            X            X             X             X
2            C            C            C             X             X
3            C            C            C             X             X
4            U            U            X             X             X
5           NA            U            X             X             X
6           NA           NA            X            NA            NA
", header=TRUE, as.is=TRUE )

d2 <- dta %>%
     gather( Model, Status ) %>%
     group_by( Model, Status ) %>%
     summarise( count = n() ) %>%
     group_by( Model ) %>%
     mutate( perc = 100 * count / sum( count )
           , percpos = rev( cumsum( rev( perc ) ) - 0.5 * rev( perc ) )
           ) %>%
     ungroup %>%
     mutate( Modelf = factor( Model )
           , percLbl = sprintf( "%2.0f%%", perc )
           )

plt <- ggplot( d2
              , aes( x = Modelf
                   , y = perc
                   , fill = Status
                   )
              ) +
     geom_bar( stat = "identity" ) +
     labs( x = "Model", y = "Percent Complete" ) +
     geom_text( mapping = aes( x = Modelf
                             , y = percpos
                             , label = percLbl )
              , vjust = 0.5 )
plt
---

On Mon, 5 Dec 2016, Shawn Way wrote:

> That?s precisely what I?m trying to accomplish.
>
> Shawn Way, PE
>
> From: John Kane [mailto:jrkrideau at yahoo.ca]
> Sent: Monday, December 05, 2016 4:47 PM
> To: Shawn Way <SWay at meco.com>; r-help at r-project.org
> Subject: Re: [R] Using ggplot2 to plot percentages in bar chart.
>
> I've never seen   stat_bin used like that. What exactly is it supposed to do. It looks like you are trying to label the %ages in each piece of the bar.
>
> On Monday, December 5, 2016 2:17 PM, Shawn Way <SWay at meco.com<mailto:SWay at meco.com>> wrote:
>
> I have the following data in which I'm trying to summarize in a stacked bar plot showing the percentages as a label in the bar.
>
> The data is as follows:
>
>> head(data)
>  MASTERPAK2LT MASTERPAK4LT MASTERPAK7LT MASTERPAK10LT MASTERPAK22LT
> 1            X            X            X            X            X
> 2            C            C            C            X            X
> 3            C            C            C            X            X
> 4            U            U            X            X            X
> 5        <NA>            U            X            X            X
> 6        <NA>        <NA>            X          <NA>          <NA>
>
> It is then transformed using dplyer:
>
>> d2 <- data %>%
>    gather(Model,Status) %>%
>    group_by(Model,Status) %>%
>    summarise(count=n()) %>%
>    mutate(perc=count/sum(count))
>
> giving
>> head (d2)
> Source: local data frame [6 x 4]
> Groups: Model [2]
>
>          Model Status count      perc
>          <chr>  <chr> <int>      <dbl>
> 1 MASTERPAK10LT      C    8 0.21052632
> 2 MASTERPAK10LT      X    29 0.76315789
> 3 MASTERPAK10LT  <NA>    1 0.02631579
> 4 MASTERPAK22LT      C    6 0.15789474
> 5 MASTERPAK22LT      U    1 0.02631579
> 6 MASTERPAK22LT      X    30 0.78947368
>
> I then try to plot this using ggplot using
>
> plt <- ggplot(d2,aes(x=Model,y= perc,fill=Status)) +
>    geom_bar(stat="identity") +
>    labs(y="Percent Complete") +
>    stat_bin(geom = "text",
>            aes(label=paste(round(perc*100),"%")),
>            vjust=5) +
>    scale_y_continuous(labels = percent)
>
> but I get the error:
>
> Error: stat_bin() must not be used with a y aesthetic.
>
> When I leave out the stat_bin, I get the correct bar chart, but without the labels.  Can someone please help me understand what is causing the error above?
>
>
> Thank you kindly,
>
> Shawn Way, PE
>
> ______________________________________________
> R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To 
> UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/p
> osting-guide.html> and provide commented, minimal, self-contained, 
> reproducible code.
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 
> 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.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list