mitchus
mitchus

Reputation: 4877

ggplot2: piecharts instead of points in an xy scatterplot

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

Upvotes: 2

Views: 2592

Answers (2)

rankthefirst
rankthefirst

Reputation: 1468

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

Upvotes: 2

mitchus
mitchus

Reputation: 4877

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

Upvotes: 1

Related Questions