Histogram: Percentage does not sum up to 100%

846 views
Skip to first unread message

Bernd Weiss

unread,
May 7, 2012, 11:09:49 AM5/7/12
to ggp...@googlegroups.com
Dear all,

The following code creates four histograms but within each panel the
percentage does not sum up to 100. How can I fix that?

library(ggplot2)
set.seed(1)
g <- rep(c(1:4), 20)
x <- round(rnorm(80), 0)
mydf <- data.frame(g,x)
prop.table(table(mydf$x, mydf$g), 2)
ggplot(aes(x=x), data=mydf) + stat_bin(aes(y =
100*((..count..)/sum(..count..))), binwidth = 3.5) + facet_wrap(~g)

Thanks,

Bernd

Winston Chang

unread,
May 7, 2012, 1:39:49 PM5/7/12
to Bernd Weiss, ggp...@googlegroups.com
Is this code, using aes(y=100*((..count..)/sum(..count..))), posted somewhere online? It has come up on this list a few times recently. I think the answer is that there isn't an easy way to do this.

In this thread I provide a workaround, where you do the binning/calculation before sending it to ggplot:

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
May 7, 2012, 6:50:28 PM5/7/12
to Bernd Weiss, ggp...@googlegroups.com
Hi:

One way to simplify the problem a bit is to convert the table of
proportions to a data frame, multiply the proportions by 100 and then
pass the result to ggplot2, as Winston suggested. Here's one way:

library('ggplot2')
u <- as.data.frame(prop.table(with(mydf, table(x, g)), 2))
u$Freq <- 100 * u$Freq
names(u) <- c('z', 'group', 'Pct')

ggplot(u, aes(x = z, y = Pct)) +
geom_histogram(stat = 'identity', fill = 'blue') +
facet_wrap( ~ group)

HTH,
Dennis

On Mon, May 7, 2012 at 8:09 AM, Bernd Weiss <bernd...@uni-koeln.de> wrote:

Bernd Weiss

unread,
May 8, 2012, 1:54:38 AM5/8/12
to Winston Chang, ggp...@googlegroups.com
On 07.05.2012 19:39, Winston Chang wrote:
> Is this code, using aes(y=100*((..count..)/sum(..count..))), posted
> somewhere online? It has come up on this list a few times recently. I
> think the answer is that there isn't an easy way to do this.
>
> In this thread I provide a workaround, where you do the
> binning/calculation before sending it to ggplot:
> https://groups.google.com/forum/?fromgroups#!topic/ggplot2/jGVzQi6Kmjk

Hi Winston,

Thanks a lot for your help!

I am currently updating an old paper and I am redoing all my R analyses
and this ggplot2-code worked 1 or 2 years ago.

For similar solutions, see

-
http://stackoverflow.com/questions/3695497/ggplot-showing-instead-of-counts-in-charts-of-categorical-variables

-http://stackoverflow.com/questions/3455190/can-qplot-directly-display-percentages-without-intermediate-columns

Thanks,

Bernd

Bernd Weiss

unread,
May 8, 2012, 1:59:43 AM5/8/12
to Dennis Murphy, ggp...@googlegroups.com
On 08.05.2012 00:50, Dennis Murphy wrote:
> Hi:
>
> One way to simplify the problem a bit is to convert the table of
> proportions to a data frame, multiply the proportions by 100 and then
> pass the result to ggplot2, as Winston suggested. Here's one way:
>
> library('ggplot2')
> u <- as.data.frame(prop.table(with(mydf, table(x, g)), 2))
> u$Freq <- 100 * u$Freq
> names(u) <- c('z', 'group', 'Pct')
>
> ggplot(u, aes(x = z, y = Pct)) +
> geom_histogram(stat = 'identity', fill = 'blue') +
> facet_wrap( ~ group)
>

Hi Dennis,

Thanks for your solution, which absolutely makes sense to me! Over the
years I have learned that it might be easier to do all data manipulation
and calculation outside of ggplot2...

Thanks,

Bernd
Reply all
Reply to author
Forward
0 new messages