ggplot2派和圆环图在同一个情节 [英] ggplot2 pie and donut chart on same plot

查看:573
本文介绍了ggplot2派和圆环图在同一个情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图复制这个

  browsers< -structure(list(browser = structure(c(3L,3L,3L, 3L,2L,2L,
2L,1L,5L,5L,4L).Label = c(Chrome,Firefox,MSIE,
Opera,Safari (5L,
6L,7L,8L,2L,3L,4L,1L,10L,11L,9L),。标签= c(Chrome 10.0 ,
Firefox 3.5,Firefox 3.6,Firefox 4.0,MSIE 6.0,MSIE 7.0,
MSIE 8.0,MSIE 9.0,Opera 11.x ,Safari 4.0,Safari 5.0
),class =factor),份额= c(10.85,7.35,33.06,2.81,1.58,
13.12,5.43,9.91,1.42,4.55 ,1.65),y max = c(10.85,18.2,51.26,
54.07,55.65,68.77,74.2,84.11,85.53,90.08,91.73),y min = c(0,
10.85,18.2,51.26 ,54.07,55.65,68.77,74.2,84.11,85.53,
90.08)),.Names = c(浏览器,版本,份额,ymax,ymin
) ,row.names = c(NA,-11L),class =data.frame)

它看起来像这样:

 >浏览器
浏览器版本份额ymax ymin
1 MSIE MSIE 6.0 10.85 10.85 0.00
2 MSIE MSIE 7.0 7.35 18.20 10.85 $ b $ 3 MSIE MSIE 8.0 33.06 51.26 18.20
4 MSIE MSIE 9.0 2.81 54.07 51.26
5 Firefox Firefox 3.5 1.58 55.65 54.07
6 Firefox Firefox 3.6 13.12 68.77 55.65
7 Firefox Firefox 4.0 5.43 74.20 68.77
8 Chrome Chrome 10.0 9.91 84.11 74.20
9 Safari Safari 4.0 1.42 85.53 84.11
10 Safari Safari 5.0 4.55 90.08 85.53
11 Opera Opera 11.x 1.65 91.73 90.08

到目前为止,我已经绘制了单个组件(即版本的圆环图和浏览器的饼图),如下所示:

  ggplot(浏览器)+ geom_rect(aes(fill = version,ymax = ymax,ymin = ymin,xmax = 4,xmin = 3))+ 
coord_polar(theta =y)+ xlim(c(0,4))

$ b $ pre $ g $ pgplot(浏览器)+ geom_bar(aes(x =因子(1),fill = browser),width = 1)+
coord_polar(theta =y)


问题是,我如何将两者结合起来看起来像最顶级的图像?我尝试了很多方法,例如:
$ b $ pre $ g $ pggplot(浏览器)+ geom_rect(aes(fill = version,ymax = ymax, (x =因子(1),fill =浏览器),width = 1)+ coord_polar(theta =y)+ xlim(c(0,ymin = ymin,xmax = 4,xmin = 3) 4))

但是我所有的结果都是扭曲的或者以错误信息结束。

解决方案

我发现首先在直角坐标系下工作更容易,如果是正确的,则切换到极坐标系。 x坐标变成极坐标的半径。因此,在直角坐标系中,内部曲线从零到数字,比如3,而外部频带从3到4. 例如



pre $ g $ pggplot(浏览器)+
geom_rect(aes(fill = version,ymax = ymax,ymin = ymin,xmax = 4,xmin = $ 3))+
geom_rect(aes(fill = browser,ymax = ymax,ymin = ymin,xmax = 3,xmin = 0))+
xlim(c(0,4))+
主题(aspect.ratio = 1)

然后,当你切换到极地时,你会得到像你一样的东西寻找。

  ggplot(浏览器)+ 
geom_rect(aes(fill = version,ymax = ymax,ymin = (b)(b)(b)(b)(b)(b)(c)(b) 0,4))+
theme(aspect.ratio = 1)+
coord_polar(theta =y)



这是一个开始,但可能需要微调对y(或角度)的依赖关系,并计算出标签/图例/着色。 ..通过对内外环使用矩形,这应该简化调整着色。另外,使用reshape2 :: melt函数重新组织数据非常有用,因此使用group(或color)可以正确显示图例。


I am trying to replicate this with R ggplot. I have exactly the same data:

browsers<-structure(list(browser = structure(c(3L, 3L, 3L, 3L, 2L, 2L, 
2L, 1L, 5L, 5L, 4L), .Label = c("Chrome", "Firefox", "MSIE", 
"Opera", "Safari"), class = "factor"), version = structure(c(5L, 
6L, 7L, 8L, 2L, 3L, 4L, 1L, 10L, 11L, 9L), .Label = c("Chrome 10.0", 
"Firefox 3.5", "Firefox 3.6", "Firefox 4.0", "MSIE 6.0", "MSIE 7.0", 
"MSIE 8.0", "MSIE 9.0", "Opera 11.x", "Safari 4.0", "Safari 5.0"
), class = "factor"), share = c(10.85, 7.35, 33.06, 2.81, 1.58, 
13.12, 5.43, 9.91, 1.42, 4.55, 1.65), ymax = c(10.85, 18.2, 51.26, 
54.07, 55.65, 68.77, 74.2, 84.11, 85.53, 90.08, 91.73), ymin = c(0, 
10.85, 18.2, 51.26, 54.07, 55.65, 68.77, 74.2, 84.11, 85.53, 
90.08)), .Names = c("browser", "version", "share", "ymax", "ymin"
), row.names = c(NA, -11L), class = "data.frame")

and it looks like this:

> browsers
   browser      version  share   ymax   ymin
1     MSIE     MSIE 6.0  10.85  10.85   0.00
2     MSIE     MSIE 7.0   7.35  18.20  10.85
3     MSIE     MSIE 8.0  33.06  51.26  18.20
4     MSIE     MSIE 9.0   2.81  54.07  51.26
5  Firefox  Firefox 3.5   1.58  55.65  54.07
6  Firefox  Firefox 3.6  13.12  68.77  55.65
7  Firefox  Firefox 4.0   5.43  74.20  68.77
8   Chrome  Chrome 10.0   9.91  84.11  74.20
9   Safari   Safari 4.0   1.42  85.53  84.11
10  Safari   Safari 5.0   4.55  90.08  85.53
11   Opera   Opera 11.x   1.65  91.73  90.08

So far, I have plotted the individual components (i.e. the donut chart of the versions, and the pie chart of the browsers) like so:

ggplot(browsers) + geom_rect(aes(fill=version, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
coord_polar(theta="y") + xlim(c(0, 4))

ggplot(browsers) + geom_bar(aes(x = factor(1), fill = browser),width = 1) +
coord_polar(theta="y")

The problem is, how do I combine the two to look like the topmost image? I have tried many ways, such as:

ggplot(browsers) + geom_rect(aes(fill=version, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +         geom_bar(aes(x = factor(1), fill = browser),width = 1) + coord_polar(theta="y") + xlim(c(0, 4)) 

But all my results are either twisted or end with an error message.

解决方案

I find it easier to work in rectangular coordinates first, and when that is correct, then switch to polar coordinates. The x coordinate becomes radius in polar. So, in rectangular coordinates, the inside plot goes from zero to a number, like 3, and the outer band goes from 3 to 4.

For example

ggplot(browsers) + 
  geom_rect(aes(fill=version, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
  geom_rect(aes(fill=browser, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) +
  xlim(c(0, 4)) + 
  theme(aspect.ratio=1) 

Then, when you switch to polar, you get something like what you are looking for.

ggplot(browsers) + 
  geom_rect(aes(fill=version, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
  geom_rect(aes(fill=browser, ymax=ymax, ymin=ymin, xmax=3, xmin=0)) +
  xlim(c(0, 4)) + 
  theme(aspect.ratio=1) +
  coord_polar(theta="y")  

This is a start, but may need to fine tune the dependency on y (or angle) and also work out the labeling / legend / coloring... By using rect for both the inner and outer rings, that should simplify adjusting the coloring. Also, it can be useful to use the reshape2::melt function to reorganize the data so then legend comes out correct by using group (or color).

这篇关于ggplot2派和圆环图在同一个情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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