美丽的饼图与R [英] beautiful Pie Charts with R

查看:263
本文介绍了美丽的饼图与R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  mydata < -  data.frame(group = c(A ,B,0,AB),FR = c(20,32,32,16))

如果我想从这个数据框创建一个饼图,我可以这样做:

  with(mydata,pie (FR,labels = paste0(as.character(group),,FR,%),radius = 1))



这很简单,但可以接受。



我怎样才能得到与ggplot2或lattice相似的东西?

经过多次反复试验,我得到了

  ggplot(mydata,aes(x = factor(1),y = FR,fill = factor(group)))+ geom_bar(width = 1,stat =identity)+ coord_polar(theta =y)



它更加复杂和丑陋。难道它不容易吗?
ggplot书籍只给出了一些例子,并且不鼓励使用饼图。



莱迪思更糟糕,您需要许多许多线路才能使它变得恐怖。



请问有人能帮我顶一张漂亮简单的饼图吗?
例如,类似...





< a href =https://i.stack.imgur.com/KIGyD.jpg =noreferrer>

没有任何R包可以轻松完成,没有20行代码?


<您可以尝试 plotrix pie3D()函数。 c $ c $> $:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ main =分解的3D饼图,explode = 0.1,radius = 0.9,labelcex = 1.2,start = 0.7)


Let's say I have this simple data:

 mydata <- data.frame(group=c("A", "B", "0", "AB"), FR=c(20, 32, 32, 16))

If I want to create a pie chart from this dataframe I can do:

 with(mydata,pie(FR, labels=paste0(as.character(group), " ", FR, "%"), radius=1))

It's quite simple but acceptable.

How can I get something similar with ggplot2 or lattice?

After much trial and error I've got

ggplot(mydata, aes(x = factor(1), y=FR,fill=factor(group)) ) + geom_bar(width = 1,stat="identity")+coord_polar(theta = "y") 

It's much more complex and ugly. Isn't it supposed to be easy? ggplot books only give some examples and discourage from using pie charts.

Lattice is even worse, you need many many lines to get it's scaring.

Could anybody help me top get a nice and simple Pie chart, please? For example something like...

Isn't there any R package able to do it easily, without 20 lines of code?

解决方案

You can try with the pie3D() function from the plotrix package:

library(plotrix)
pie3D(mydata$FR, labels = mydata$group, main = "An exploded 3D pie chart", explode=0.1, radius=.9, labelcex = 1.2,  start=0.7)

这篇关于美丽的饼图与R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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