Barplot with table but not with data.frame?

632 views
Skip to first unread message

Peng Yu

unread,
Aug 3, 2011, 12:12:14 PM8/3/11
to ggplot2
Hi All,

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

Adam_L...@keybank.com

unread,
Aug 3, 2011, 1:04:33 PM8/3/11
to Peng Yu, ggplot2

This works:

x<-rnorm(1000)
y<-rnorm(1000)
z<-cbind(x,y)

ggplot(data=NULL,aes(z[,1],z[,2]))+geom_point()

This doesn't:

ggplot(data=z[,c('x','y')],aes(x,y))+geom_point()
Error: ggplot2 doesn't know how to deal with data of class matrix




Adam Loveland




Peng Yu <peng...@gmail.com>
Sent by: ggp...@googlegroups.com

08/03/2011 12:12 PM

To
ggplot2 <ggp...@googlegroups.com>
cc
Subject
Barplot with table but not with data.frame?





--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

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




This communication may contain privileged and/or confidential information. It
is intended solely for the use of the addressee. If you are not the intended
recipient, you are strictly prohibited from disclosing, copying, distributing
or using any of this information. If you received this communication in error,
please contact the sender immediately and destroy the material in its entirety,
whether electronic or hard copy. This communication may contain nonpublic personal
information about consumers subject to the restrictions of the 
Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose
such information for any purpose other than to provide the services for which
you are receiving the information.

127 Public Square, Cleveland, OH 44114



If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.

Brandon Hurr

unread,
Aug 3, 2011, 1:17:12 PM8/3/11
to Adam_L...@keybank.com, Peng Yu, ggplot2
I don't quite understand why, but the table function is summarizing the data. I got it to give me the same plot with the following code:

ggplot(data= as.data.frame(table(diamonds[,c('color', 'cut')])))+ geom_bar(aes(fill=cut, x= color, y=Freq, stat='identity'))

As Adam pointed out, ggplot isn't usually very happy with things that aren't dataframes. 

Brian Diggs

unread,
Aug 3, 2011, 1:39:39 PM8/3/11
to ggplot2

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

Peng Yu

unread,
Aug 3, 2011, 4:46:36 PM8/3/11
to Brian Diggs, ggplot2
On Wed, Aug 3, 2011 at 12:39 PM, Brian Diggs <dig...@ohsu.edu> wrote:
> On 8/3/2011 9:12 AM, Peng Yu wrote:
>>
>> Hi All,
>>
>> 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')])
>
> 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")

I want to understand this command better. Do you know where "Freq" is
documented?

--
Regards,
Peng

Dennis Murphy

unread,
Aug 3, 2011, 5:52:12 PM8/3/11
to Peng Yu, ggplot2
Example:

> 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

Reply all
Reply to author
Forward
0 new messages