ggplot饼图
目录:
- 原始图样
- 如何去除饼图中心的杂点
- 如何去除饼图旁边的标签
- 如何去掉左上角多出来的一横线
- 如何去掉图例的标题,并将图例放到上面
- 如何对图例的标签加上百分比
- 如何让饼图的小块按顺时针从大到小的顺序显示
- 如何去掉白色外框上的数字
- 如何在图中加百分比
- 如何生成饼环
(更多内容请见:R、ggplot2、shiny 汇总)
原始图样:
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

版权声明:转载请注明出处,谢谢!
ggplot饼图的更多相关文章
- 用ggplot包画一个简单饼图
首先用library函数加载ggplot2包 library(ggplot2) library(dplyr) library(tidyr) library(splines) 接下来,进行数据准备: d ...
- How To Use ggplot in ggplot2?
1.What is ggplot2 ggplot2基本要素 数据(Data)和映射(Mapping) 几何对象(Geometric) 标尺(Scale) 统计变换(Statistics) 坐标系统(C ...
- R绘图 第八篇:绘制饼图(ggplot2)
geom_bar()函数不仅可以绘制条形图,还能绘制饼图,跟绘制条形图的区别是坐标系不同,绘制饼图使用的坐标系polar,并且设置theta="y": coord_polar(th ...
- R笔记4:ggplot绘制商务图表--玫瑰图
我们说Excel有难度的图表,可以考虑ggplot2是否更方便,本帖的例子就是用ggplot做玫瑰图. Excel做玫瑰图有一定难度,可以使用雷达图或圆环图来构建,我的博客上曾有多个帖子讨论这个,见 ...
- r画饼图
原始图样: library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot ...
- 关于matplotlib,你要的饼图在这里
Table of Contents 1 官方Demo 2 将实际数据应用于官方Demo 3 一些改善措施 3.1 重新设置字体大小 3.2 设置显示颜色,Method 1: 3.3 设置显 ...
- ggplot绘图之基本语法
ggplot绘图之基本语法 2018年09月03日 22:29:56 一个人旅行*-* 阅读数 4332更多 分类专栏: R语言 1.ggplot2图形之基本语法: ggplot2的核心理念是将绘 ...
- 数据可视化基础专题(十二):Matplotlib 基础(四)常用图表(二)气泡图、堆叠图、雷达图、饼图、
1 气泡图 气泡图和上面的散点图非常类似,只是点的大小不一样,而且是通过参数 s 来进行控制的,多的不说,还是看个示例: 例子一: import matplotlib.pyplot as plt im ...
- 读取数据库数据,并将数据整合成3D饼图在jsp中显示
首先我将生成饼图的方法独立写成一个PieChar.java类,详细代码如下:(数据库需要自己建,如有需要的话) import java.io.IOException; import java.sql. ...
随机推荐
- SAP接口设计的扩展性考虑
由于现在的系统和SAP的接口出现了几次变更,因此需要对系统进行设计改造.由于系统中和SAP交互的接口不止一处,而且也是在不同的时间段进行开发,并由不同的人员来完成的,因此我在维护升级的 ...
- ant批量运行Jmeter脚本遇到 Content is not allowed in prolog.问题及解决方案
在执行 最后生成报告的 task 时,一直报下面这个错: TransformerException, Content is not allowed in prolog. 解决方法:需要修改jmete ...
- VS报:"dll标记为系统必备组件,必须对其进行强签名"错误
问题: VS生成程序时,报“要将程序集“XX.dll”标记为系统必备组件,必须对其进行强签名.”错误. 解决方法: 1)在报错的解决方案中找到一个可以发布的项目(引用该XX.dll的项目未必可以发布) ...
- 最常用的五类CSS选择器
一些新手朋友对选择器一知半解,不知道在什么情况下运用什么样的选择器,这是一个比较头疼的问题,针对新手朋友,对CSS选择器作一些简单的说明,希望能对大家的学习工作有一定的帮助,更多的CSS知识请参考We ...
- Linux使用技巧5--格式化U盘
通常来说,格式化一个分区的U盘还是非常easy的.仅仅须要使用mkfs命令指定目标文件系统就能够了,样例例如以下: $ sudo fdisk -l $ sudo mkfs -t vfat /dev/s ...
- Oracle免费版和付费版,各版本的说明
Oracle免费版和付费版的区别: 首先这里给出一个答案,oracle确实是免费的给学习的人员使用.oracle的策略就是你可以随意下载我的产品,包括oracle,weblogic等产品用于学习, ...
- git版本控制文件提交到composer应用市场,并下载用市场的软件库
要把github中的项目提交到composer中去,必须在github管理的项目中新建对应的composer.json文件, composer.json文件建立的方法 cmd定位到项目目录 compo ...
- ORACLE生成唯一标识函数
-- Create tablecreate table TAB_TEST( id VARCHAR2(40) not null, fxnum VARCHAR2(40)) ---------------- ...
- [k8s]kubespray(ansible)自动化安装k8s集群
kubespray(ansible)自动化安装k8s集群 https://github.com/kubernetes-incubator/kubespray https://kubernetes.io ...
- [svc]免费证书认证
1.申请免费域名(12个月) freenom.com 2.申请免费证书(3个月) git clone https://github.com/letsencrypt/letsencrypt cd let ...