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

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