Lauren Maynard
Lauren Maynard

Reputation: 65

Faceted Pie Charts

I'm having issues with faceted pie charts.

Here's the head of my dataset.

> head(pie)

          piper         prop      bat
1  P. sancti-felicis 0.617593433  Cs
2     P. reticulatum 0.617593433  Cs
3       P. colonense 0.069142205  Cs
4 P. multiplinervium 0.007650765  Cs
5        P. peltatum 0.006790803  Cs
6       P. umbricola 0.102377661  Cs

And here is the code I'm attempting...

a <- ggplot(pie, aes(x = piper,y=prop, fill = piper)) +
+   geom_bar(width = 1,stat='identity')
b <- a + coord_polar(theta = "y", start = 0, direction = 1)
b + facet_grid(facets=. ~ bat)

enter image description here

It seems to be displaying in a coxcomb pie chart, when I want to show a regular pie chart separating piper by color. The three pie charts are separated correctly by bat. Would greatly appreciate suggestions!

Upvotes: 1

Views: 119

Answers (1)

Claus Wilke
Claus Wilke

Reputation: 17790

It would help a lot if you could provide your entire dataset or a made-up/simplified version of it (see also here). Otherwise it's difficult to figure out what exactly the problem is or even what the figure should look like in the first place.

Anyways, I'm working with the six lines of data you provided:

df <- read.table(text = "piper         prop      bat
  'P. sancti-felicis' 0.617593433  Cs
     'P. reticulatum' 0.617593433  Cs
       'P. colonense' 0.069142205  Cs
 'P. multiplinervium' 0.007650765  Cs
        'P. peltatum' 0.006790803  Cs
       'P. umbricola' 0.102377661  Cs", header = TRUE)

#               piper        prop bat
#1  P. sancti-felicis 0.617593433  Cs
#2     P. reticulatum 0.617593433  Cs
#3       P. colonense 0.069142205  Cs
#4 P. multiplinervium 0.007650765  Cs
#5        P. peltatum 0.006790803  Cs
#6       P. umbricola 0.102377661  Cs

One immediate probelm is that the proportions don't add to one, so it's not clear how you want to plot them in a pie chart. However, disregarding this issue, we can assume that the proportions are not normalized and that we need to normalize them ourselves (by dividing by the total sum of proportions). If this is correct, then we can draw a pie chart like this:

ggplot(df, aes(x = 1, y = prop, fill = piper)) + 
  geom_col(position = "stack") + facet_wrap(~bat) +
  coord_polar(theta = "y") + theme_minimal()

enter image description here

I faceted by bat, but since there is only one in my dataset there is only pie in the result. I also used theme_minimal() to remove much of the distracting gray background etc., but you can use any theme you like.

Upvotes: 1

Related Questions