I'm trying to compare two pie chart from ggplot2,but when I try to graph it because of the big difference in length of the dataset It look very small.
I wanted the piechart divided by a category and group by another.
df %>%
count(category,group) %>%
ggplot(aes(x="", y=n, fill=factor(group)))
geom_bar(stat="identity", width=1)
coord_polar("y", start=0)
facet_wrap(~category)
I did the following to normalize their height.
Piechartdf <- df %>%
count(category,group)
Piechart <- Piechart %>%
mutate(proportion = case_when(
Type=="category1" ~ n/sum(Piechart[which(Piechart$Type == "category1"), 3]),
Type=="category2" ~ n/sum(Piechart[which(Piechart$Type == "category2"), 3])
))
Then in the first one, I just chance n for proportion, Is there an easier way to achieve this?
CodePudding user response:
You may try
df %>%
count(category,group) %>%
group_by(category) %>%
mutate(n = n/sum(n)) %>%
ggplot(aes(x="", y=n, fill=factor(group), group = category))
geom_bar(stat="identity", width=1)
coord_polar("y", start=0)
facet_wrap(~category)