How to draw this bar plot with precomputed statistics?

160 views
Skip to first unread message

Dejian Zhao

unread,
Jun 4, 2014, 12:58:48 PM6/4/14
to ggplot2
I ran a program and got a count summary. Now I want to generate a figure
like the one produced with the following command:

ggplot(mtcars,aes(factor(cyl))) + geom_bar(aes(fill=factor(vs)),
position="fill")

The only difference between my own data and the above example is that I
got the count summary from another program. It is mentioned in Hadley's
book "ggplot2: Elegant Graphics for Data Analysis" how to display
precomputed statistics, but there is no example in the book. The
description of the section is as below:

"4.9.2 Displaying precomputed statistics
If you have data which has already been summarised, and you just want to
use it, you’ll need to use stat_identity(), which leaves the data
unchanged, and then map the appropriate variables to the appropriate
aesthetics."

I tried without success. Does anyone know how to draw the figure?

My dataset is like this:

>df <- data.frame(sample=c(rep("case1",3), rep("case2",3),
rep("case3",3)), group=rep(letters[1:3],3), countSum=11:19)

> df
sample group countSum
1 case1 a 11
2 case1 b 12
3 case1 c 13
4 case2 a 14
5 case2 b 15
6 case2 c 16
7 case3 a 17
8 case3 b 18
9 case3 c 19


The figure I want is like the one produced with the above-mentioned
first command, with "sample" corresponding to "factor(cyl)", "group" to
"factor(vs)", and "countSum" to "count".

Thanks!

Dejian

Roman Luštrik

unread,
Jun 4, 2014, 1:05:50 PM6/4/14
to Dejian Zhao, ggplot2
I think you're after `stat = "identity"`. See http://docs.ggplot2.org/current/geom_bar.html

Cheers,
Roman




--
--
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+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

--- You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.

Ben Bond-Lamberty

unread,
Jun 4, 2014, 1:09:25 PM6/4/14
to Roman Luštrik, Dejian Zhao, ggplot2
...following on Roman's comment, you thus probably want something like

ggplot(df,aes(sample,countSum)) + geom_bar(aes(fill=group),stat='identity')

Ben
>> To unsubscribe: email ggplot2+u...@googlegroups.com
>> More options: http://groups.google.com/group/ggplot2
>>
>> --- You received this message because you are subscribed to the Google
>> Groups "ggplot2" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to ggplot2+u...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> In God we trust, all others bring data.
>
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.

Dejian Zhao

unread,
Jun 4, 2014, 2:44:43 PM6/4/14
to ggplot2, Ben Bond-Lamberty, Roman Luštrik
Yes, that's what I want. Thanks a lot! -Dejian
Reply all
Reply to author
Forward
0 new messages