目录:

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

(更多内容请见: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. LINUX使用FTP搭建网络版YUM源

    在YUM,FTP服务器上配置(192.168.56.2) .安装.配置vsftp # rpm -ivh vsftpd--.el6.x86_64 # chkconfig vsftpd on # serv ...

  2. org.hibernate.MappingException: entity class not found hbm可以解析,但是实体类不能解析

    在hbm.xml中给实体类加上包  com.we.lkl.studentVO

  3. java mac install

    http://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html#A1096855 This page descri ...

  4. Android利用Fiddler进行网络数据抓包【怎么跟踪微信请求】

    主要介绍Android及IPhone手机上如何利用Fiddler进行网络数据抓包,比如我们想抓某个应用(微博.微信.墨迹天气)的网络通信请求就可以利用这个方法. Mac 下请使用 Charles 代替 ...

  5. 为什么easyui的datagrid里getSelections还有getChecked只能获取一行值呢?

    http://jquery-easyui.wikidot.com/forum/t-280470 如果没有 idField属性,getSelections就只能获取一个行的值了 注意:如果用了前面说的& ...

  6. 使用perldoc阅读perl文档

    perl在安装的时候,就给我们送上一份大礼,组织精美,解释详细的perl百科全书已经安装在你的电脑里面了,遇到问题不要在去搜索那些博客了,还是练练英文,看看perldoc吧,呵呵. 1.用perldo ...

  7. php 读取功能分割大文件实例详解

    在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数.但当所操作的文件是一个比较大的文件时,这些函数可能就显的力不从心, 下面将从一个需求 ...

  8. HTTP请求中的Keep-Alive模式详解

    Keep-Alive模式 我们都知道HTTP是基于TCP的,每一个HTTP请求都需要进行三步握手.如果一个页面对某一个域名有多个请求,就会进行频繁的建立连接和断开连接.所以HTTP 1.0中出现了Co ...

  9. 经纬度、时分秒转换的C#类

    一:新建一个包含经纬度.时间转换的类optaDataConvert public class optaDataConvert { /// <summary> /// 时间转换 /// &l ...

  10. maven(6)------maven坐标分析

    在不使用maven管理项目,直接使用IDE开发项目时,一个web项目中会涉及到很多技术, 比如struts2,hibernate,spring,mybatis等等,这个时候,我们就需要去各大官网下载不 ...