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.
Here is the stacked chart

And here you can download the data
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.
+ 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.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.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())

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

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With