create floating pie charts with ggplot

2020-07-22 18:36发布

问题:

Currently I am working on report with a floating pie chart as one of the plots. Currently I use the plotrix package to plot this pie chart. But since I use ggplot2 for all the other plots, the pie plot looks different. So I'm trying to create the plot using ggplot2. Currently I have two issues:

  1. The ggplot pie charts are plotted on a polar coordination system. I want to plot multiple pie charts on Cartesian coordination system. At the moment I do not know how to do this.

  2. I want to be able to control the pie radius on the Cartesian coordination system.

Here is the code I currently use:

library("plotrix")
plot(1:5, type="n", xlab="x", ylab="y")
floating.pie(2, 3, c(1,3,5), radius=0.5)
floating.pie(4, 2, c(2,4), radius=0.2)
floating.pie(4.5, 4, c(3,2,5,1), radius=0.3)

Thanks for your time and help.

回答1:

  1. Pie charts by definition use polar coordinates. You could overlay some pie charts on another graph that used Cartesian coordinates but it would probably be awful. In fact, pie charts are mostly awful anyway, so be careful what you wish for.

  2. An example on the coord_polar page.

The important bit in that code is specifying that radius maps to the "y" aesthetic.

 df <- data.frame(
   variable = c("resembles", "does not resemble"),
   value = c(80, 20)
 )
ggplot(df, aes(x = "", y = value, fill = variable)) + 
  geom_bar(width = 1, stat = "identity") + 
  scale_fill_manual(values = c("red", "yellow")) + 
  coord_polar("y", start = 2 * pi / 3) +    #<- read this line!
  ggtitle("Pac man")


回答2:

I had the same issue, there is a package called scatterpie based on ggfore solving the problem.

It's on cran, and you can see the examples here