0

I have reached to create a melted dataframe containing as values the % of the energy sources (factor variable) for several Years, as additional factor or Date: enter image description here

How could I make nice faceted pie charts for the different years with ggplot (or plotrix)?

So, far, I have reached to:

ggplot(melted_df, aes(x=Year, y=Share, fill=Source)) +
  geom_bar(stat="identity", width=1)+
  coord_polar("y", start=0) +
  geom_text(aes(label = paste0(round(Share*100), "%")), position = position_stack(vjust = 0.5),size=3)+
  labs(x = NULL, y = NULL, fill = NULL, title = "Energy Mix")+
  theme_classic() + theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          plot.title = element_text(hjust = 0.5, color = "#666666"))

which without the facet command gives this, which is not aesthetically pleasant: enter image description here

while if I add the facet_wrap(~Year) command, it becomes worse... enter image description here

choabf
  • 57
  • 5
  • Hard to demonstrate solution w/o sample data for us to use. I'd try `aes(x=1...` instead of `x=Year` or `facet_wrap(~Year, scales = free_x)` – Jon Spring Dec 19 '21 at 03:06
  • Use `dput(...)` to get a pasteable version of the data. The image of your data isn't very helpful. Also, pie charts are hard to read and it is almost always better to use a bar chart. – Michael Dewar Dec 19 '21 at 14:11
  • Thank you so much, Jon Spring. It worked. Now, I just wonder how to put the piechart labels outside the slices. Thanks again! – choabf Dec 19 '21 at 14:24

1 Answers1

0

After commentators suggestion, aes(x=1) in the ggplot() line solves the issue and makes normal circle parallel pies:

ggplot(melted_df, aes(x=1, y=Share, fill=Source)) +
  geom_col(width=1,position="fill", color = "black")+
  coord_polar("y", start=0) +
  geom_text(aes(x = 1.7, label = paste0(round(Share*100), "%")), size=2, 
            position = position_stack(vjust = 0.5))+
  labs(x = NULL, y = NULL, fill = NULL, title = "Energy Mix")+
  theme_classic() + theme(axis.line = element_blank(),
                          axis.text = element_blank(),
                          axis.ticks = element_blank(),
                          plot.title = element_text(hjust = 0.5, color = "#666666"))+
  facet_wrap(~Year)
choabf
  • 57
  • 5
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 19 '21 at 21:46