creating a simple pie chart

2,099 views
Skip to first unread message

Data Analytics Corp.

unread,
Sep 29, 2010, 12:32:54 PM9/29/10
to ggp...@googlegroups.com
Hi,

I have a simple problem that I just cannot get - I need to create a
simple pie chart. My data frame is

data <- data.frame(cluster = paste("Cluster", 1:4), size =
c(0.5519, 0.2238, 0.1544, 0.0698))

where "size" is the proportion of consumers in each cluster (and sum to
1.0). The pie should have four slices, each the size of "size" and the
legend should simply be the cluster names from "cluster". How do I do
this????

Also, how do I get labels on the slices (e.g., "Cluster 1: 0.5519")?

Thanks,

Walt

________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ 08536
________________________
(V) 609-936-8999
(F) 609-936-3733
wa...@dataanalyticscorp.com
www.dataanalyticscorp.com

_____________________________________________________


--
________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ 08536
________________________
(V) 609-936-8999
(F) 609-936-3733
wa...@dataanalyticscorp.com
www.dataanalyticscorp.com

Heike Hofmann

unread,
Sep 29, 2010, 12:41:55 PM9/29/10
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
Walt,

following Hadley's example in the coord_polar help:

pie <- ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +
 geom_bar(width = 1) + coord_polar(theta="y")
pie

HTH,
Heike

--
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

Data Analytics Corp.

unread,
Sep 29, 2010, 1:04:25 PM9/29/10
to ggp...@googlegroups.com
Hi,

This helped. I was missing the "weight = size". Now for labels on the
slices.

Thanks,

Walt

________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ 08536
________________________
(V) 609-936-8999
(F) 609-936-3733
wa...@dataanalyticscorp.com
www.dataanalyticscorp.com

_____________________________________________________

>> wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>

Brandon Hurr

unread,
Sep 30, 2010, 4:36:24 AM9/30/10
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
Walt, 

It's not pretty, but I used geom_text() to put some labels on there. I had to hand edit the position in though, which means that you'd probably be better off with sigmaplot or Excel (*shudder*).

data <- data.frame(cluster = paste("Cluster", 1:4), size = c(0.5519, 0.2238, 0.1544, 0.0698), position = c(0.5519, 0.5519+0.2238, 0.5519+0.2238+0.1544, 0.5519+0.2238+0.1544+0.0698), position2 = c(0.3, 0.7, 0.85, 0.965))

ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +
geom_bar(width = 1) + geom_text(aes(y=position2, label=size))+coord_polar(theta="y")

Brandon

James Howison

unread,
Sep 30, 2010, 10:54:37 AM9/30/10
to ggplot2
Or throwing the labels up there somewhere off the chart, then moving them with Inkscape?

>>>> To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com>


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


>>> More options: http://groups.google.com/group/ggplot2
>>>
>>
>>
>> --
>> ________________________
>>
>> Walter R. Paczkowski, Ph.D.
>> Data Analytics Corp.
>> 44 Hamilton Lane
>> Plainsboro, NJ 08536
>> ________________________
>> (V) 609-936-8999
>> (F) 609-936-3733
>> wa...@dataanalyticscorp.com
>> www.dataanalyticscorp.com
>>
>> --
>> 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<ggplot2%2Bunsu...@googlegroups.com>

Data Analytics Corp.

unread,
Sep 30, 2010, 12:15:05 PM9/30/10
to ggp...@googlegroups.com
Hi,

I also played around with geom_text but then gave up since it became too
much work for a simple problem. Pie charts are used all the time in
business, so it would seem to me that being able to create them in
ggplot2 would be a easy. But not so. From a business perspective, this
limits ggplot2. Incidentally, the pie chart has an annoying ring around
it showing the degrees of the circle. This is a nuisance that would be
very difficult to explain to a client. Another limitation.

Thanks,

Walt

________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ 08536
________________________
(V) 609-936-8999
(F) 609-936-3733
wa...@dataanalyticscorp.com
www.dataanalyticscorp.com

_____________________________________________________

> wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>
> www.dataanalyticscorp.com <http://www.dataanalyticscorp.com>

> <mailto:wa...@dataanalyticscorp.com
> <mailto:wa...@dataanalyticscorp.com>>
>
> www.dataanalyticscorp.com <http://www.dataanalyticscorp.com>


>
> _____________________________________________________
>
>
> --
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733

> wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>
> www.dataanalyticscorp.com <http://www.dataanalyticscorp.com>


>
> --
> 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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>


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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>


> More options: http://groups.google.com/group/ggplot2
>
>
>
> --
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733

> wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>
> www.dataanalyticscorp.com <http://www.dataanalyticscorp.com>


>
> --
> 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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>

Data Analytics Corp.

unread,
Sep 30, 2010, 12:16:16 PM9/30/10
to ggp...@googlegroups.com
If I have to use SigmaPlot, Excel or Inkscape, then what's the purpose
of ggplot2????

Brandon Hurr

unread,
Sep 30, 2010, 12:24:47 PM9/30/10
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
Sigmaplot/Excel was a joke on my part. 

The following code gives you a very stripped down pie. Looks pretty decent if you ask me. 

data <- data.frame(cluster = paste("Cluster", 1:4), size = c(0.5519, 0.2238, 0.1544, 0.0698), position = c(0.5519, 0.5519+0.2238, 0.5519+0.2238+0.1544, 0.5519+0.2238+0.1544+0.0698), position2 = c(0.3, 0.7, 0.85, 0.965))

ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +
geom_bar(width = 1) + geom_text(aes(y=position2, label=size))+coord_polar(theta="y")+theme_bw() + opts(axis.title.x = theme_blank(), axis.title.y= theme_blank(), axis.text.x=theme_blank(), axis.text.y=theme_blank(), panel.grid.minor = theme_blank(), panel.grid.major = theme_blank(), panel.border = theme_blank())


I suppose the reason pie charts are so hard is that it's probably underutilized in everywhere but business (where it is overused IMO). With more use, more work will be put into it. Aside from the labels, I think we're getting pretty darn close don't you think Walt? 

Brandon

Brandon Hurr

unread,
Sep 30, 2010, 12:34:17 PM9/30/10
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
I'm awesome. 

data <- data.frame(cluster = paste("Cluster", 1:4), size = c(0.5519, 0.2238, 0.1544, 0.0698), position = c(0.5519, 0.5519+0.2238, 0.5519+0.2238+0.1544, 0.5519+0.2238+0.1544+0.0698), position2 = c(0.3, 0.7, 0.85, 0.965))

ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +
geom_bar(width = 1) + geom_text(aes(x= 1.75, y=position2, label=size))+coord_polar(theta="y")+theme_bw() + opts(axis.title.x = theme_blank(), axis.title.y= theme_blank(), axis.text.x=theme_blank(), axis.text.y=theme_blank(), panel.grid.minor = theme_blank(), panel.grid.major = theme_blank(), panel.border = theme_blank(), axis.ticks=theme_blank())

/applause

I set the geom_text() with an x value of 1.75 and it puts it just outside of the circle. I also got rid of that random tick mark that was in the previous graph. 

As far as the labels go. I think you could create a script of sorts that takes the percent of the pie or decimal of each section and places a position at 1/2 of the width. so... 0.55 / 2 = pos1... 0.55 + 0.22/2 = pos2... etc...

Brandon
awesome.pie.png

James Howison

unread,
Sep 30, 2010, 12:42:57 PM9/30/10
to ggplot2
Very nice :)

I see how position is calculated, that could be generalized. Perhaps to a geom_pie

How do you calculate position2, is that trial and error for placing the labels?

>>>>>>>> <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Buns...@googlegroups.com>


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

>>>>>>> <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Buns...@googlegroups.com>


>>>>>>>>
>>>>>>> More options: http://groups.google.com/group/ggplot2
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> --
>>>>>> ________________________
>>>>>>
>>>>>> Walter R. Paczkowski, Ph.D.
>>>>>> Data Analytics Corp.
>>>>>> 44 Hamilton Lane
>>>>>> Plainsboro, NJ 08536
>>>>>> ________________________
>>>>>> (V) 609-936-8999
>>>>>> (F) 609-936-3733
>>>>>> wa...@dataanalyticscorp.com
>>>>>> www.dataanalyticscorp.com
>>>>>>
>>>>>> --
>>>>>> 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<ggplot2%2Bunsu...@googlegroups.com>

>>>>>> <ggplot2%2Bunsu...@googlegroups.com<ggplot2%252Buns...@googlegroups.com>

> <awesome.pie.png>

Data Analytics Corp.

unread,
Sep 30, 2010, 12:54:44 PM9/30/10
to ggp...@googlegroups.com
Now this is worth studying. But it should be codified as geom_pie since
labels are a standard part of pie charts.

Walt

> <wa...@dataanalyticscorp.com <mailto:wa...@dataanalyticscorp.com>>

> <mailto:wa...@dataanalyticscorp.com>> wrote:
>
>
> Hi,
>
> This helped. I was missing the "weight = size".
> Now for labels on the
> slices.
>
>
> Thanks,
>
> Walt
>
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733
> wa...@dataanalyticscorp.com

> _____________________________________________________
>
>
> --
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733
> wa...@dataanalyticscorp.com

> --
> 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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email
> ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com><ggplot2%2Bunsu...@googlegroups.com
> <mailto:ggplot2%252Buns...@googlegroups.com>>


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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email
> ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com><ggplot2%2Bunsu...@googlegroups.com
> <mailto:ggplot2%252Buns...@googlegroups.com>>


> More options:
> http://groups.google.com/group/ggplot2
>
>
>
> --
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733
> wa...@dataanalyticscorp.com

> --
> 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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email
> ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com><ggplot2%2Bunsu...@googlegroups.com
> <mailto:ggplot2%252Buns...@googlegroups.com>>


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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email
> ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>


> More options: http://groups.google.com/group/ggplot2
>
>
>
>
> --
> ________________________
>
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733

> --
> 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

> <mailto:ggp...@googlegroups.com>
> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@googlegroups.com>

Joshua Wiley

unread,
Sep 30, 2010, 1:03:39 PM9/30/10
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
If all you want is a pie chart, why not use base?

# I renamed your data object because data is also a function name
dat <- data.frame(cluster = paste("Cluster", 1:4), size = c(0.5519,
0.2238, 0.1544, 0.0698))

# pie chart with labels
# (side puzzle, which one of these colours is out of place?)
with(dat, pie(x = size, labels = paste(cluster, size, sep = ": "),
col = c("tomato3", "honeydew3", "papayawhip", "mintcream")))


# 4, 'mintcream'. Tomatoes, honeydew melons, and papayas are all
fruits, mintcream is not.

Cheers,

Josh


On Thu, Sep 30, 2010 at 9:16 AM, Data Analytics Corp.
<wa...@dataanalyticscorp.com> wrote:
> If I have to use SigmaPlot, Excel or Inkscape, then what's the purpose of
> ggplot2????

Consider these plots:

x <-c(.22, .25, .28, .25)
pie(x)
plot(x=1:4, y = x, ylim = c(0, .4))

x2 <- sample(x, 8, replace = TRUE)
pie(x2)
plot(x = 1:8, y = x2, ylim = c(0, .4))

These are not beautiful examples, but at least for me, it is much
slower and harder to read the pie chart than the scatter plot.
ggplot2 will easily do the scatter plot (and make it look much nicer
than the crass thing I presented here).

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

Brandon Hurr

unread,
Sep 30, 2010, 1:07:34 PM9/30/10
to James Howison, ggplot2
James,
I did the labels by hand in this case because I wasn't sure how to get
R to calculate what I wanted, quickly.

Something like this...

xy<-data.frame(x=c(60, 25, 15), y=c(0,0,0))

t<-length(xy[[1]]

xy$y<-1/2*xy$x

for (i in 2:length(xy[[1]])){
xy$y[i]<-xy$y[i]+xy$x[i-1]
}

I know that's probably a horrible example, but it is 'an' example.

Although, Josh has a good point.

Brandon

Data Analytics Corp.

unread,
Sep 30, 2010, 1:10:39 PM9/30/10
to ggp...@googlegroups.com
Yes, I could have used the function in base, but then why have ggplot2?
Besides, I want to also create plots using the facet command and so the
pie should have the same look and feel as the facet plot.

Walt

Ben Bolker

unread,
Sep 30, 2010, 1:52:45 PM9/30/10
to ggp...@googlegroups.com
The reason it probably can't be geom_pie() is that (as created in
this way) it's not really its own
geom, it's a geom_bar() in polar coordinates. geom_pie() would be
something closer to the floating.pie()
function in the plotrix package.
A reason to write a geom_pie() from scratch would be to enable
functionality like floating.pie (which is
used, for example in population genetics, to display breakdown of allele
proportions at different geographic locations).
This would have to have aesthetics x,y, (location on the plot) fill
(slice colours) colour (border colours), proportion (slice areas),
text (labels),size ... Writing new non-trivial geoms looks tricky
though, at least to me.

For what it's worth, there's a small counter-literature (the Spence
2005 reference <http://www.psych.utoronto.ca/users/spence/Spence%202005.pdf>
from the wikipedia 'pie chart' article is a starting point) that says
pie charts aren't that bad
(although Few <http://www.perceptualedge.com/articles/08-21-07.pdf>, who
tries to be pretty thorough,
finds only one small case where they're provably better).

There's something to be said for culture, too. I think the example
above from population genetics isn't
that bad -- I don't think you'd gain that much by re-doing them as bar
charts or stacked bars.

takahashi kohske

unread,
Sep 30, 2010, 1:54:47 PM9/30/10
to wa...@dataanalyticscorp.com, ggp...@googlegroups.com
here is an excellent example ;-p

data <- data.frame(cluster = paste("Cluster", 1:4), size = c(0.5519,
0.2238, 0.1544, 0.0698))

data$p<-(p<-cumsum(data$size)-diff(c(0,cumsum(data$size)))*(1-0.5)) #
is this simplest?

ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +

geom_bar(width=1) + geom_text(aes(x= 1.6, y=p,
angle=-p*360,label=size),vjust=0)+coord_polar(theta="y")

- you can control the distance between pie and text by changing x=1.6
- you can control the position of the text along the pie by changing 1-0.5
- you can also control the angle of the text.

On Fri, Oct 1, 2010 at 2:10 AM, Data Analytics Corp.

Erik Iverson

unread,
Sep 30, 2010, 2:03:59 PM9/30/10
to ggp...@googlegroups.com

Data Analytics Corp. wrote:
> Yes, I could have used the function in base, but then why have ggplot2?

Because choice, in general, is good.

Several tools leads to best chance that what I want can be handled by
one of them.

I've found ggplot2 the best tool for my graphics needs, but I've never
used it to make a pie chart.

--Erik

Reply all
Reply to author
Forward
0 new messages