ggplot2: piecharts instead of points in an xy scatterplot

寵の児 提交于 2019-12-19 10:22:31

问题


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


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




回答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



来源:https://stackoverflow.com/questions/7836279/ggplot2-piecharts-instead-of-points-in-an-xy-scatterplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!