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:
How do I fill the center?
Any other improvements appreciated.
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")
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))
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:
A clear demonstration that it's better to see the differences between the categories when you use a barchart instead of a piechart.
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: