Skip to content
Back to formatted view

Raw Message

Message-ID: <1464040087.465506.1481026022227@mail.yahoo.com>
Date: 2016-12-06T12:07:02Z
From: John Kane
Subject: Using ggplot2 to plot percentages in bar chart.
In-Reply-To: <52c127b98314438a8bba73c28e4b145e@CTC-HOU-EXMB-02.ctcloud.local>

Well personally I would not have a clue on how to approach the problem but have a look at http://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2. which seems to do what you want only using geom_text()

Best of luck.
 ? 





 

    On Monday, December 5, 2016 5:56 PM, Shawn Way <SWay at meco.com> wrote:
 

 #yiv4374957155 #yiv4374957155 -- _filtered #yiv4374957155 {font-family:Helvetica;panose-1:2 11 6 4 2 2 2 2 2 4;} _filtered #yiv4374957155 {font-family:SimSun;panose-1:2 1 6 0 3 1 1 1 1 1;} _filtered #yiv4374957155 {panose-1:2 4 5 3 5 4 6 3 2 4;} _filtered #yiv4374957155 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;} _filtered #yiv4374957155 {panose-1:2 1 6 0 3 1 1 1 1 1;}#yiv4374957155 #yiv4374957155 p.yiv4374957155MsoNormal, #yiv4374957155 li.yiv4374957155MsoNormal, #yiv4374957155 div.yiv4374957155MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:12.0pt;}#yiv4374957155 a:link, #yiv4374957155 span.yiv4374957155MsoHyperlink {color:blue;text-decoration:underline;}#yiv4374957155 a:visited, #yiv4374957155 span.yiv4374957155MsoHyperlinkFollowed {color:purple;text-decoration:underline;}#yiv4374957155 span.yiv4374957155EmailStyle17 {color:#1F497D;}#yiv4374957155 .yiv4374957155MsoChpDefault {font-size:10.0pt;} _filtered #yiv4374957155 {margin:1.0in 1.0in 1.0in 1.0in;}#yiv4374957155 div.yiv4374957155WordSection1 {}#yiv4374957155 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> 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 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.



   
	[[alternative HTML version deleted]]