library(ggplot2)
qplot(color, data=diamonds, geom ='bar', fill=cut, position='fill')
The above code plot a barplot. But suppose that I only have the
following table available, but not the original data.frame (diamonds),
could anybody let me know how to plot the same barplot?
table(diamonds[,c('color', 'cut')])
--
Regards,
Peng
Peng Yu <peng...@gmail.com>
Sent by: ggp...@googlegroups.com 08/03/2011 12:12 PM |
|
diamond.table <- as.data.frame(table(diamonds[,c('color', 'cut')]))
ggplot(diamond.table, aes(color, Freq)) +
geom_bar(aes(fill=cut), statistic="identity", position="fill")
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
I want to understand this command better. Do you know where "Freq" is
documented?
--
Regards,
Peng
> with(chickwts, table(feed))
feed
casein horsebean linseed meatmeal soybean sunflower
12 10 12 11 14 12
> dd <- as.data.frame.table(with(chickwts, table(feed)))
> dd
feed Freq
1 casein 12
2 horsebean 10
3 linseed 12
4 meatmeal 11
5 soybean 14
6 sunflower 12
In this simple one-factor case, table returns a named vector of
frequencies assigned a class 'table'. as.data.frame.table() takes the
table, converts the factors on each margin into variables (something
like expand.grid()), and creates a variable Freq to carry along the
cell frequencies. A more appropriate example in the multiway table
case would be UCBAdmissions, a 3D table. To convert it to a data
frame, use
as.data.frame(UCBAdmissions)
Dennis