r画饼图
原始图样:
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画饼图的更多相关文章
- 使用R画地图数据
用R画地图数据 首先,从这里下载中国地图的GIS数据,这是一个压缩包,完全解压后包含三个文件(bou2_4p.dbf.bou2_4p.shp和bou2_4p.shx),将这三个文件解压到同一个目录下. ...
- (转)浅谈HTML5与css3画饼图!
神马系饼图? 饼图,大家都应该熟知,在统计数据对比方面,几乎处处用到.如cnzz的统计饼图 从饼图中,很形象地展示了访问者地区的分布,以扇形为块的方式拼成一个大圆. 都使用什么方法实现 目前众多站点制 ...
- R画网络图
R 画网络图 目的:用R做生信分析,画基因样本的网络图,从中观察样本的致病性情况. 一.所用到的包 library(tidyr) library(ggplot2) library(reshape2) ...
- 用 R 画中国分省市地图
用 R 画中国分省市地图 (2010-11-18 16:25:34) 转载▼ 标签: 中国地图 营销 杂谈 分类: 数据分析 用R 也可以做出漂亮的依参数变化的中国地图. 主要参考(http://co ...
- R语言-饼图
1.饼图 > browsers<-read.table("browsers.txt",header=TRUE) > browsers<-browsers[o ...
- 使用matplotlib画饼图
import matplotlib.pyplot as pltx = [4, 9, 21, 55, 30, 18]labels = ['math', 'history', 'chemistry', ' ...
- Matplotlib学习---用matplotlib画饼图/面包圈图(pie chart, donut chart)
我在网上随便找了一组数据,用它来学习画图.大家可以直接把下面的数据复制到excel里,然后用pandas的read_excel命令读取.或者直接在脚本里创建该数据. 饼图: ax.pie(x,labe ...
- 用R画韦恩图
#导入R包 library(grid)library(futile.logger)library(VennDiagram) #建立测试数据集 A = 1:150B = c(121:170,300:32 ...
- 使用HTML5画饼图
在进行数据的统计分析时, 饼图也是比较经常用到的一类统计图. 需求分析: 一个饼图一般包含以下几部分: 1.标题 2.扇形 3.份额(百分比) 4.标识器 设计: ...
随机推荐
- Linux系统休眠和设备中断处理
一.设备IRQ的suspend和resume 本小节主要解决这样一个问题:在系统休眠过程中,如何suspend设备中断(IRQ)?在从休眠中唤醒的过程中,如何resume设备IRQ? 一般而言,在系统 ...
- 离线安装 Ambari Hadoop
制作本地yum源 安装步骤: 先建立本地yum源(Ambari和HDP的) 1. 先安装 ambari 然后http://localhost:8080 登录进去,设置 hdp的源链接. 2. 安装HD ...
- Spring AOP之Introduction(@DeclareParents)简介(转)
Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...
- Socket tips: 同意socket发送UDP Broadcast
假设创建一个UDP Socket: socketHandle = socket(serverAddr->ai_family, serverAddr->ai_socktype, server ...
- OPENGL NEHE Lesson11 11课的计算公式推导
计算多边形公式推导: 条件x 离散的值从0到45; y离散的值从0到45; z是符合正弦波. 问题: 1 要求x’=f(x)映射到 x’ ∈[-4.5, 4.5], x ∈{0, 1, 2, …, 4 ...
- IP首部
1. 引言 IP是TCP/IP协议族中最为核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输,但是IP提供不可靠.无连接的数据报传送服务.不可靠的意思是它不能保证IP数据报 ...
- 【Android】11.3 屏幕旋转和场景变换过程中GridView的呈现
分类:C#.Android.VS2015: 创建日期:2016-02-21 一.简介 实际上,对于布局文件中的View来说,大多数情况下,Android都会自动保存这些状态,并不需要我们都去处理它.这 ...
- css3导航hover悬停效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- rpc简介、原理、实例
简介 RPC(Remote Procedure Call,远程过程调用)是建立在Socket之上的,出于一种类比的愿望,在一台机器上运行的主程序,可以调用另一台机器上准备好的子程序,就像LPC(本地过 ...
- form的method用get导致中文乱码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...