2

I have a four-dimensional dataset, let's call the variables x, y, z and r. There is at most one entry for each combination of x and y. Currently, I have a scatterplot where I plot for each entry a point at position (x,y) and size z.

ggplot(aes(x=x,y=y)) + geom_point(aes(size=z))

This produces a nice plot, but now I would like to "upgrade" it as follows: I want to replace each point with a piechart, and the piechart should be the same size as the point. The pie consists of two sections, one blue, and orange, taking up a proportion of r of the pie (r always being between 0 and 1).

Any ideas on how to do this elegantly?

Cheers

joran
  • 169,992
  • 32
  • 429
  • 468
mitchus
  • 4,677
  • 3
  • 35
  • 70
  • 1
    This thread should get you going: http://stackoverflow.com/questions/2181902/how-to-use-an-image-as-a-point-in-ggplot – John Colby Oct 20 '11 at 15:04
  • 2
    I think you'll have to [write a new geom](https://github.com/hadley/ggplot2/wiki/Creating-a-new-geom), and probably a new scale as well. – baptiste Oct 21 '11 at 04:57
  • Thanks for the link John; if I just add a `stat_spoke` on top of my current graph, it does the trick nicely! – mitchus Oct 21 '11 at 08:49
  • if you're willing to use base graphics instead of ggplot2 see `floating.pie` in the `plotrix` package ... – Ben Bolker Oct 21 '11 at 13:06

2 Answers2

2

You can use ggforce to realize the function, but someone has already done this for you, and created another R package called scatterpie, you can find it here

rankthefirst
  • 1,370
  • 2
  • 14
  • 26
1

This does the trick:

ggplot(aes(x=x,y=y)) + geom_point(aes(size=z)) + stat_spoke(aes(angle=r*2*pi, radius=3*z))

mitchus
  • 4,677
  • 3
  • 35
  • 70
  • I see how you get two dimensions from that, but it isn't a pie chart per se. I didn't see any pie chart examples at http://docs.ggplot2.org/current/stat_spoke.html – David J. Mar 31 '13 at 04:46
  • @DavidJames You are correct, it is not a piechart. In my case, since I just wanted to express a proportion `r` for each point, the spoke is fine. – mitchus Apr 01 '13 at 15:25