原始图样:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity") +  coord_polar(theta = "y") ## 把柱状图折叠成饼图(极坐标) p

如何去除饼图中心的杂点:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) + ## width >= 1 时中心的杂点将消失 coord_polar(theta = "y")  p

如何去除饼图旁边的标签:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") ## 将标签设为空 p

如何去掉左上角多出来的一横线:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) ## 把左上角多出来的“小胡子”去掉 p

如何去掉图例的标题,并将图例放到上面:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) +  theme(legend.title = element_blank(), legend.position = "top") ## 将图例标题设为空,并把土方放在上方 p

如何对图例的标签加上百分比:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E'))  myLabel = as.vector(dt$B) ## 转成向量,否则图例的标签可能与实际顺序不一致 myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%) ", sep = "") ## 用 round() 对结果保留两位小数  p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) +  theme(legend.title = element_blank(), legend.position = "top") +  scale_fill_discrete(breaks = dt$B, labels = myLabel) ## 将原来的图例标签换成现在的myLabel p

如何让饼图的小块按顺时针从大到小的顺序显示:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E'))  dt = dt[order(dt$A, decreasing = TRUE),] ## 用 order() 让数据框的数据按 A 列数据从大到小排序 myLabel = as.vector(dt$B)  myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%) ", sep = "")   p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) +  theme(legend.title = element_blank(), legend.position = "top") +  scale_fill_discrete(breaks = dt$B, labels = myLabel)  p

如何去掉白色外框上的数字:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E'))  dt = dt[order(dt$A, decreasing = TRUE),] ## 用 order() 让数据框的数据按 A 列数据从大到小排序 myLabel = as.vector(dt$B)  myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%) ", sep = "")   p = ggplot(dt, aes(x = "", y = A, fill = B)) +  geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) +  theme(legend.title = element_blank(), legend.position = "top") +  scale_fill_discrete(breaks = dt$B, labels = myLabel) +  theme(axis.text.x = element_blank()) ## 白色的外框即是原柱状图的X轴,把X轴的刻度文字去掉即可 p

如何在图中加百分比:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E'))  dt = dt[order(dt$A, decreasing = TRUE),] myLabel = as.vector(dt$B)  myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%)", sep = "")   p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) +  coord_polar(theta = "y") +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) +  theme(legend.title = element_blank(), legend.position = "top") +  scale_fill_discrete(breaks = dt$B, labels = myLabel) +  theme(axis.text.x = element_blank()) +  geom_text(aes(y = A/2 + c(0, cumsum(A)[-length(A)]), x = sum(A)/20, label = myLabel), size = 5) ## 在图中加上百分比:x 调节标签到圆心的距离, y 调节标签的左右位置 p

如何生成饼环:

library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E'))  dt = dt[order(dt$A, decreasing = TRUE),] myLabel = as.vector(dt$B)  myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%)", sep = "")   p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 0.3) + ## 当width < 1 时饼图将变成饼环  coord_polar(theta = "y") +  theme_bw() +  labs(x = "", y = "", title = "") +  theme(axis.ticks = element_blank()) +  theme(legend.position = "none") +  theme(axis.text.x = element_blank()) +  geom_text(aes(y = A/2 + c(0, cumsum(A)[-length(A)]), x = sum(A)/24, label = myLabel), size = 5)  p

r画饼图的更多相关文章

  1. 使用R画地图数据

    用R画地图数据 首先,从这里下载中国地图的GIS数据,这是一个压缩包,完全解压后包含三个文件(bou2_4p.dbf.bou2_4p.shp和bou2_4p.shx),将这三个文件解压到同一个目录下. ...

  2. (转)浅谈HTML5与css3画饼图!

    神马系饼图? 饼图,大家都应该熟知,在统计数据对比方面,几乎处处用到.如cnzz的统计饼图 从饼图中,很形象地展示了访问者地区的分布,以扇形为块的方式拼成一个大圆. 都使用什么方法实现 目前众多站点制 ...

  3. R画网络图

    R  画网络图 目的:用R做生信分析,画基因样本的网络图,从中观察样本的致病性情况. 一.所用到的包 library(tidyr) library(ggplot2) library(reshape2) ...

  4. 用 R 画中国分省市地图

    用 R 画中国分省市地图 (2010-11-18 16:25:34) 转载▼ 标签: 中国地图 营销 杂谈 分类: 数据分析 用R 也可以做出漂亮的依参数变化的中国地图. 主要参考(http://co ...

  5. R语言-饼图

    1.饼图 > browsers<-read.table("browsers.txt",header=TRUE) > browsers<-browsers[o ...

  6. 使用matplotlib画饼图

    import matplotlib.pyplot as pltx = [4, 9, 21, 55, 30, 18]labels = ['math', 'history', 'chemistry', ' ...

  7. Matplotlib学习---用matplotlib画饼图/面包圈图(pie chart, donut chart)

    我在网上随便找了一组数据,用它来学习画图.大家可以直接把下面的数据复制到excel里,然后用pandas的read_excel命令读取.或者直接在脚本里创建该数据. 饼图: ax.pie(x,labe ...

  8. 用R画韦恩图

    #导入R包 library(grid)library(futile.logger)library(VennDiagram) #建立测试数据集 A = 1:150B = c(121:170,300:32 ...

  9. 使用HTML5画饼图

    在进行数据的统计分析时, 饼图也是比较经常用到的一类统计图. 需求分析:   一个饼图一般包含以下几部分:   1.标题   2.扇形   3.份额(百分比)   4.标识器      设计:     ...

随机推荐

  1. vs开发 winform 设置winform 获取管理员权限启动

    因为需要设置为开机项 没有管理员权限对注册表访问失败 C# 以管理员身份运行WinForm程序 转载https://www.bbsmax.com/A/obzbkKrQJE/ 鱼洛 2016-07-29 ...

  2. 《JAVA与模式》之桥接模式

    桥接模式是一种结构型模式,它主要应对的是:由于实际的需要,某个类具有两个或两个以上的维度变化,如果只是用继承将无法实现这种需要,或者使得设计变得相当臃肿. 桥接模式的做法是把变化部分抽象出来,使变化部 ...

  3. PSAPI和ToolHelpAPI学习笔记

    标 题: PSAPI学习笔记 作 者:北极星2003 时 间:2005-07-24 18:36 链 接:http://bbs.pediy.com/showthread.php?threadid=154 ...

  4. Sunisoft.IrisSkin.SkinEngine 设置winform皮肤

    Sunisoft.IrisSkin.SkinEngine se; se = new Sunisoft.IrisSkin.SkinEngine { SkinAllForm = true, SkinFil ...

  5. php将session保存到数据库的类实例(php版本需要大于5.4)

    这里实现了一个把session存储到数据库的类,包括数据表的创建.类的使用.php的配置. 可以更好地进行用户控制管理. 做项目的时候,有一个需求,是要实现禁止一个账号两处登录.同时要统计当前在线用户 ...

  6. 在C#中使用WMI查询进程的用户信息

    这是一个使用WMI查询信息的例子.看之前请对WMI有一个简单的了解,可以百度,或者查看我上一篇:WMI测试器 主要代码:(需要添加对System.Management的引用) //创建Win32_Pr ...

  7. Python shell对比

    对Python.shell的一些思考 如果使用python去写脚本来处理日常事务的话,相对于shell是一件比较麻烦的事情,因为我可以使用shell在花费更少的时间内,比较熟练地使用awk.sed和g ...

  8. SDUT 1941-Friday the Thirteenth(水)

    Friday the Thirteenth Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述  Is Friday the 13 ...

  9. C# 获取指定目录下所有文件信息

    /// <summary> /// 返回指定目录下所有文件信息 /// </summary> /// <param name="strDirectory&quo ...

  10. python(28)获得网卡的IP地址,如何在其他文件夹中导入python模块

    获得第几块网卡的ip地址: 如何在其他文件夹中导入模块 import sys sys.path.append('/search/chen/tool')#你的代码存放的目录 from Get_Ip im ...