0

Attempting to create pie chart with ggplot2 but cannot seem to get it using other references online. The chart I create is missing most of its fill.

ggplot(sae,aes(x=1,fill=factor(State), width=1))+        
geom_bar()+
ggtitle("House by State")+
coord_polar(theta='y')

This code gives:

enter image description here

How do I fill the center?

Any other improvements appreciated.

Nick Chandler
  • 77
  • 1
  • 10
  • 2
    Could you give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)? – Jaap Sep 03 '15 at 17:46
  • 2
    Piecharts are bad. But you can use `x=factor(1)` if you really want one. – Heroka Sep 03 '15 at 17:50
  • @Heroka Using `1` or `factor(1)` doesn't make a difference. What I'm missing is an `y` inside the `aes`. – Jaap Sep 03 '15 at 17:53
  • 1
    @Jaap I made some sample data based on some assumptions on what the data looked like. Worked on my machine, with my assumptions. geom_bar doesn't have `y` as a required aesthetic. – Heroka Sep 03 '15 at 17:55
  • http://docs.ggplot2.org/current/coord_polar.html – user20650 Sep 03 '15 at 17:56
  • See [this example](http://stackoverflow.com/questions/16184188/ggplot-facet-piechart-placing-text-in-the-middle-of-pie-chart-slices/22804400#22804400) for making a (faceted) pie-chart. – Jaap Sep 03 '15 at 17:56
  • working on a reproducible example right now. – Nick Chandler Sep 03 '15 at 17:57
  • But I will repeat myself. Piecharts are bad. What is wrong with a (stacked) barchart? [hope this is not a paywall](http://onlinelibrary.wiley.com/doi/10.1002/sim.6549/abstract?campaign=woletoc) – Heroka Sep 03 '15 at 18:03
  • @Heroka Completely agree that pie-charts are bad, I dedicated an answer to that. – Jaap Sep 03 '15 at 18:13
  • @Jaap I will edit my answer with some more examples. Hard to differentiate between 'interesting programming puzzle!' and 'people should stop doing this'... – Heroka Sep 03 '15 at 18:16

3 Answers3

5

With sample data

sae <- data.frame(State=sample(LETTERS[1:6],60,T))

                  
ggplot(sae,aes(x=factor(1),fill=factor(State)))+        
  geom_bar(width=1)+
  ggtitle("House by State")+
  coord_polar(theta="y")

enter image description here

EDIT: Other options (because piecharts are bad)

#following Jaaps example: some better way to visualize this
#grouped barchart

p1 <- ggplot(sae, aes(x=State, fill=State)) +
  geom_bar() + labs(title="grouped barchart")


#stacked barchart; especially practical if you want to compare groups
sae$group <- rbinom(60,1,0.5)
p2 <- ggplot(sae, aes(x=factor(group),fill=State))+
  geom_bar(width=0.5) + labs(title="grouped stacked barchart")

do.call(grid.arrange,list(grobs=list(p1,p2),ncol=2))

enter image description here

Community
  • 1
  • 1
Heroka
  • 12,889
  • 1
  • 28
  • 38
  • Good answer, +1. Keep in mind though that this way of constructing piecharts (without specifying `y`) only works for factor variables. – Jaap Sep 03 '15 at 18:22
  • Thanks! Now I'm trying to think of situations of pie-charts with continuous y-axis. And I should stop thinking about piecharts (and continue my thesis). – Heroka Sep 03 '15 at 18:47
2

As @Heroka already mentioned in the comments, pie-charts are a bad way of visualizing information. They are bad that it is even mentioned in the help-files of R.

From ?pie:

Pie charts are a very bad way of displaying information. The eye is good at judging linear measures and bad at judging relative areas. A bar chart or dot chart is a preferable way of displaying this type of data.

Cleveland (1985), page 264: “Data that can be shown by pie charts always can be shown by a dot chart. This means that judgements of position along a common scale can be made instead of the less accurate angle judgements.” This statement is based on the empirical investigations of Cleveland and McGill as well as investigations by perceptual psychologists.

Some further reading on the pie-chart debate.


With the example data of @Heroka:

ggplot(sae,aes(x = factor(1), fill = factor(State)))+        
  geom_bar(width = 1, position = "dodge")+
  ggtitle("House by State")

you get:

enter image description here

A clear demonstration that it's better to see the differences between the categories when you use a barchart instead of a piechart.

Jaap
  • 81,064
  • 34
  • 182
  • 193
2

When you want to show information about proportions, there is another choice, the waffle package which gets back more to what you probably intend to show with a pie chart (i.e., proportions). In most instances, the bar plots above would likely be best, but for the sake of showing another way of plotting...

Using the sae data from above:

library(waffle)  # install the package if you don't have it
w <- table(sae)
w.waf <- waffle(table(sae))
w.waf + ggtitle("Contextless Waffle Graph") + theme(plot.title=element_text(face="bold", size=24))

which yields this:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
Woodstock
  • 367
  • 2
  • 6