3

I wanted to reproduce this attached graph to multiple pie-charts whose radii are defined by the total weed weight. This was the code I used:

weedweights<-data%>%
  select(-ends_with("No"))%>%
     gather(key=species, value=speciesmass, DIGSAWt:POLLAWt)%>%
     mutate(realmass= (10*speciesmass) / samplearea.m.2.)%>%
     group_by(Rot.Herb, species)%>%
     summarize(avgrealmass=mean(realmass, na.rm=TRUE))%>%
     filter(avgrealmass != "NaN")%>%
     ungroup()

ggplot(weedweights, aes(x=Rot.Herb, y=avgrealmass, fill=species, width=Rot.Herb)) +
  geom_bar(position = "fill", stat="identity") +
  facet_grid(Rot.Herb ~ .) + 
  coord_polar("y") +
  theme(axis.text.x = element_blank())

I wanted 18 pie-charts, sort of look like this but got this blank plot. Please see where the problem is. enter image description here

Here is the stacked chart

enter image description here

And here you can download the data

Community
  • 1
  • 1
Little Bee
  • 1,175
  • 2
  • 13
  • 22
  • @Jaap: Thanks very much for editing – Little Bee Jul 20 '15 at 20:09
  • 1
    You must be more specific as to your desired output. Do you want one pie chart per Rot.Herb? per species? in the example you linked, there were additional variables specifying which (row, column) each pie chart was in - do you require that, or will any ordering/placement do? Also, you mention that the width should vary by "total weed weight" but your `weedweight` dataframe only has `avgrealmass` - how are the two related? (finally, in a reproducible example you should provide the link to the `weedweight` dataframe, not the original `data` that we then have to transform). – mathematical.coffee Jul 21 '15 at 00:35

1 Answers1

6

There are quite a few problems with this. The main one is that your + is in the wrong place, but even once this is fixed there a some problems you must think about.

  • put the + at the end of the preceding line, not the start of the first. Otherwise the first line ggplot(...) looks like a complete statement (how is R to know there is a + on the next line?) i.e. ggplot(...) + on the first line, and function_call(...) + on subsequent lines so R knows there is a continuation.
  • opts is deprecated. Use theme instead.
  • theme_blank is deprecated. Use element_blank instead.
  • "At least one layer must contain all variables used for facetting" - you are faceting by avgrealmass1 ~ avgrealmass2, but none of your layers has these variables in it, and neither does your data frame. I am unsure what the purpose of this is.
  • you have width=kg.ha but weedweights doesn't have a kg.ha column.

Fixing (or omitting) these problems yields:

ggplot(weedweights, aes(x=Rot.Herb, y=avgrealmass, fill=species)) +#, width=kg.ha)) +
geom_bar(position = "fill", stat="identity") +
#facet_grid(avgrealmass1~avgrealmass2) +
coord_polar("y") +
theme(axis.text.x = element_blank())

enter image description here

This doesn't look quite what you are after, but at least it fixes a lot of your initial question, and now you can have a proper think about what it is you want to plot/facet so that you can fix the rest.


Edit update in response to OP completely changing original question...

This does one pie chart per Rot.Herb -> change your facet_grid(Rot.Herb ~ .) to facet_wrap( ~ Rot.Herb). Also remove the width=Rot.Herb from the original ggplot call because how can you map width (a number) to Rot.Herb (a string)? Note also the aes(x=1...) --> the x is just a dummy variable, doesn't matter what it is.

ggplot(weedweights, aes(x=1, y=avgrealmass, fill=species)) +
  geom_bar(position = "fill", stat="identity") +
  facet_wrap(~ Rot.Herb) + 
  coord_polar("y") +
  theme(axis.text.x = element_blank())

Now you say you want to vary the width of each pie chart by its total weed weight (though you do not explain how to obtain this, as weedweights only has one column avgrealmass).

I'll put on my mind-reading hat and assume that "total weed weight" is the sum of "avgrealmass" for each Rot.Herb (i.e. sum across all species for each Rot.Herb). If it is not, you are capable of calculating this column yourself already (you have already shown you can do this when you calculated your weedweight from your original data - well done). So, you just add this width column to your weedweights:

ww2 <- weedweights %>%
         group_by(Rot.Herb) %>%
         mutate(totalweedweight=sum(avgrealmass)) %>%
         ungroup()

Then ggplot as before with the following changes:

  • width=totalweedweight: add the width column to the ggplot call.
  • x=totalweedweight/2: all this does is ensure each pie chart is "left-aligned", as it were, i.e. the pie is a circle and not a ring (leave it as x=1 and you will see what I mean).

    ggplot(ww2, aes(x=totalweedweight/2, y=avgrealmass, fill=species, width=totalweedweight)) +
      geom_bar(position = "fill", stat="identity") +
      facet_wrap(~ Rot.Herb) + 
      coord_polar("y") +
      theme(axis.text.x = element_blank())
    

enter image description here

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
  • Thank you very much for your clarification. I monkey-ed around with the code but can't solve the problem. I'll add the new code and new problem to the original question – Little Bee Jul 20 '15 at 20:08
  • Your new question is entirely different to the old one, so now the record is confused (my answer is to your old question). Leave it for now since it's already done, but in future when you radically change your question, you should ask a new one. with a reproducible example. – mathematical.coffee Jul 21 '15 at 00:26
  • OK, I have answered your new question. – mathematical.coffee Jul 21 '15 at 00:49
  • Thanks so much! Asking a good question is convenient for everyone. I'll be asking better questions next time – Little Bee Jul 21 '15 at 15:41
  • Why is your legend different than mine? I have a continual ribbon of colors while you have them separated with a thin white line. I'm using the latest R version – Little Bee Jul 22 '15 at 17:54