原始图样:

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. Python学习笔记016——面向对象

    面向对象是指用类来描述一个对象(实例),用类来建立实例与实例的关联关系 对象 : object     实例 : instance 1 类 1.1 什么是类 类是用来描述对象的工具,用类可以创建一个或 ...

  2. 什么是Asterisk,它如何帮助我们的呼叫中心?

    如今的呼叫中心与过去的呼叫中心有很大差异.过去,一间房屋或一座大楼,装上硬接线的POTS电话,招聘几名员工就可以建立一个呼叫中心.如今,这样的情形已经一去不复返,因为有许多新技术让呼叫中心变得更像是一 ...

  3. GL_总账完整会计周期业务(流程)

    2014-06-02 BaoXinjian

  4. LevelDB场景分析2--Open

    1.源码 1 Status DB::Open(const Options& options, const std::string& dbname,      uint64_t new_ ...

  5. Python 字典 keys() 方法

    描述 Python 字典 keys() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回字典中的所有的键. 语法 keys() 方法语法: D.keys() 参数 无. 返回值 ...

  6. Objective-C学习笔记(四)——OC实现最简单的数学运算

    本篇帖子会实现使用OC的最简单的加减乘除运算.学习的知识点包含变量定义.运算方法,格式化输出等概念.主要学习主要的语法,事实上和C语言的语法还是比較相似的. 具体代码仅仅要写在main方法中即可了.具 ...

  7. vim设置文本宽度

    'textwidth' 'tw'        number  (default 0)                        local to buffer                   ...

  8. Python并发编程实例教程

    有关Python中的并发编程实例,主要是对Threading模块的应用,文中自定义了一个Threading类库. 一.简介 我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态 ...

  9. Linux下修改时间时区的方法介绍

    点评:在Linux中,用于时钟查看和设置的命令主要有date.hwclock和clock.其中,clock和 hwclock用法相近,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件 ...

  10. ORACLE自定义顺序排序

    ORACLE可以借助DECODE函数,自定义顺序排序: select * from ( select 'Nick' as item from dual union all select 'Viki' ...