目录:

  • 原始图样
  • 如何去除饼图中心的杂点
  • 如何去除饼图旁边的标签
  • 如何去掉左上角多出来的一横线
  • 如何去掉图例的标题,并将图例放到上面
  • 如何对图例的标签加上百分比
  • 如何让饼图的小块按顺时针从大到小的顺序显示
  • 如何去掉白色外框上的数字
  • 如何在图中加百分比
  • 如何生成饼环

(更多内容请见: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饼图的更多相关文章

  1. 用ggplot包画一个简单饼图

    首先用library函数加载ggplot2包 library(ggplot2) library(dplyr) library(tidyr) library(splines) 接下来,进行数据准备: d ...

  2. How To Use ggplot in ggplot2?

    1.What is ggplot2 ggplot2基本要素 数据(Data)和映射(Mapping) 几何对象(Geometric) 标尺(Scale) 统计变换(Statistics) 坐标系统(C ...

  3. R绘图 第八篇:绘制饼图(ggplot2)

    geom_bar()函数不仅可以绘制条形图,还能绘制饼图,跟绘制条形图的区别是坐标系不同,绘制饼图使用的坐标系polar,并且设置theta="y": coord_polar(th ...

  4. R笔记4:ggplot绘制商务图表--玫瑰图

    我们说Excel有难度的图表,可以考虑ggplot2是否更方便,本帖的例子就是用ggplot做玫瑰图. Excel做玫瑰图有一定难度,可以使用雷达图或圆环图来构建,我的博客上曾有多个帖子讨论这个,见 ...

  5. r画饼图

    原始图样: library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot ...

  6. 关于matplotlib,你要的饼图在这里

    Table of Contents 1  官方Demo 2  将实际数据应用于官方Demo 3  一些改善措施 3.1  重新设置字体大小 3.2  设置显示颜色,Method 1: 3.3  设置显 ...

  7. ggplot绘图之基本语法

    ggplot绘图之基本语法 2018年09月03日 22:29:56 一个人旅行*-* 阅读数 4332更多 分类专栏: R语言   1.ggplot2图形之基本语法: ggplot2的核心理念是将绘 ...

  8. 数据可视化基础专题(十二):Matplotlib 基础(四)常用图表(二)气泡图、堆叠图、雷达图、饼图、

    1 气泡图 气泡图和上面的散点图非常类似,只是点的大小不一样,而且是通过参数 s 来进行控制的,多的不说,还是看个示例: 例子一: import matplotlib.pyplot as plt im ...

  9. 读取数据库数据,并将数据整合成3D饼图在jsp中显示

    首先我将生成饼图的方法独立写成一个PieChar.java类,详细代码如下:(数据库需要自己建,如有需要的话) import java.io.IOException; import java.sql. ...

随机推荐

  1. redis 学习札记4-sortset

    redis 学习笔记4--sortset redis学习笔记3--sortSet 终于到最后一个数据结构了,加油!! 整体结构图: http://dl.iteye.com/upload/picture ...

  2. AP_标准预付款核销基本操作(流程)

    2014-06-04 Created By BaoXinjian

  3. pip 安装

    pip安装 1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads   2.下载pip-7.1.2.tar.gz (md5, pgp ...

  4. 如何在virtualenv虚拟环境中安装mysql-python

    接触过virtualenv后,想在这个虚拟环境中安装独立的开发环境.在安装MySQLdb时遇到错误 pc 09:09:30 File "/home/pc/work/VENV/py3/loca ...

  5. MATLAB(4)——图片保存方法汇总及常用指令

    作者:桂. 时间:2017-03-03  19:30:03 链接:http://www.cnblogs.com/xingshansi/p/6498318.html 前言 本文为MATLAB系列第四篇. ...

  6. unity Transform.TransformPoint

    正如unity api文档所说: Transforms position from local space to world space. 即Transform.TransformPoint是将局部坐 ...

  7. ps换衣服

    1.抠衣服->给衣服新建图层(ctrl+j)->给图层去色,让衣服边黑白色.2.剪贴蒙版(上图:大花布,下形:衣服).作用:大花布替换成衣服3.大花布图层->叠加图层样式或其它图层样 ...

  8. MySQL获取刚插入的数据

    1. 通过自增的键auto_increment取得. select max(id) from tablename 这样的做法须要考虑并发的情况.须要在事务中对主表加以"X锁",待获 ...

  9. 多重连接的数据库管理工具:Navicat Premium

    多重连接的数据库管理工具:Navicat Premium 2016-09-26 Navicat Premium(非免费软件)是一个可多重连接的数据库管理工具,它可让你以单一程序同时连接到MySQL.O ...

  10. NIS & Kerberos配置

    NIS & Kerberos配置 所需RPM包列表: krb5-server-1.10.3-42.el6.x86_64.rpm krb5-workstation-1.10.3-42.el6.x ...