使用ggplot2来表示散点图中每个点的小饼图 [英] tiny pie charts to represent each point in an scatterplot using ggplot2

查看:281
本文介绍了使用ggplot2来表示散点图中每个点的小饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个散点图,每个点都是一个小饼图。例如,考虑以下数据:

$ $ $ $ $ $ $ c $ foo < - data.frame(X = runif(30),Y = runif(30 ),A = runif(30),B = runif(30),C = runif(30))

以下代码将绘制散点图,表示每个点的 X Y 值:

  library(reshape2)
library(ggplot2)
foo.m< - melt(foo,id.vars = c(X,Y))
ggplot(foo.m,aes(X,Y))+ geom_point()



以下代码将为每个点创建一个饼图:

  p < -  ggplot(foo .m,aes(variable,value,fill = variable))+ geom_bar(stat =identity)
p + coord_polar()+ facet_wrap(〜X + Y ,, ncol = 6)+ theme_bw()



但我期待合并它们:创建一个散点图,其中每个点都被饼图取代。这样我就可以在同一个图表中显示每条记录的所有5个值(X,Y,A,B,C)。

它是什么?

解决方案

这是您可以使用package ggsubplot 完成的事情。不幸的是,根据问题10 包含有关 ggsubplot 和更新的R版本。


I want to create a scatter plot, in which each point is a tiny pie chart. For instance consider following data:

foo <- data.frame(X=runif(30), Y=runif(30),A=runif(30),B=runif(30),C=runif(30))

The following code will make a scatter plot, representing X and Y values of each point:

library(reshape2)
library(ggplot2)
foo.m <- melt(foo, id.vars=c("X","Y"))
ggplot(foo.m, aes(X,Y))+geom_point()

And the following code will make a pie chart for each point:

p <- ggplot(foo.m, aes(variable,value,fill=variable)) + geom_bar(stat="identity")
p + coord_polar() + facet_wrap(~X+Y,,ncol=6) + theme_bw()

But I am looking to merge them: creating a scatter plot in which each point is replaced by the pie chart. This way I will be able to show all 5 values (X, Y, A, B, C) of each record in the same chart.

Is there anyway to do it?

解决方案

This is the sort of thing you can do with package ggsubplot. Unfortunately, according to issue #10 here, this package is not working with R 3.1.1. I ran it successfully if I used an older version of R (3.0.3).

Using your long dataset, you could put bar plots at each X, Y point like this:

library(ggplot2)
library(ggsubplot)

ggplot(foo.m) +
    geom_subplot2d(aes(x = X, y = Y, 
                    subplot = geom_bar(aes(variable, value, fill = variable), stat = "identity")),
                width = rel(.5), ref = NULL)

This gives the basic idea, although there are many other options (like controlling where the subplots move to when there is overlap in plot space).

This answer has more information on the status of ggsubplot with newer R versions.

这篇关于使用ggplot2来表示散点图中每个点的小饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆