ggplot2:饼图而不是xy scatterplot中的点 [英] ggplot2: piecharts instead of points in an xy scatterplot
问题描述
我有一个四维数据集,我们称之为变量x,y,z和r。 x和y的每个组合最多只有一个条目。
目前,我有一个散点图,其中我为每个条目绘制了位置(x,y)和大小为z的点。
ggplot(aes(x = x,y = y))+ geom_point(aes(size = z))
这产生了一个很好的情节,但现在我想升级它,如下所示:我想用饼图替换每个点,并且饼图应该与点的大小相同。馅饼由两部分组成,一部分是蓝色,一部分是橙色,占馅饼比例(r始终在0到1之间)。
如何做到这一点优雅?
干杯
诀窍:
ggplot(aes(x = x,y = y))+ geom_point(aes(size = z))+ stat_spoke(aes (角度= r * 2 * pi,半径= 3 * z))
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
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))
这篇关于ggplot2:饼图而不是xy scatterplot中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!