ggplot2--饼状图
原文出处:http://blog.csdn.net/bone_ace/article/details/47455363
原始图样:
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.2) + ## 当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.5, label = myLabel), size = 5)
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.2) + 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.5, label = myLabel), size = 5) +theme(panel.grid=element_blank()) + ## 去掉白色圆框和中间的坐标线theme(panel.border=element_blank()) ## 去掉最外层正方形的框框
p
饼环的另外一种画法(geom_rect()):
library(ggplot2)ad = data.frame(type = c("Poster", "Billboard", "Bus", "Digital"),n = c(529, 356, 59, 81))
ad$fraction = ad$n / sum(ad$n)
ad$ymax = cumsum(ad$fraction)
ad$ymin = c(0, head(ad$ymax, n = -1))ggplot(data = ad, aes(fill = type, ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +geom_rect(colour = "grey30", show_guide = FALSE) +coord_polar(theta = "y") +labs(x = "", y = "", title = "") + xlim(c(0, 4)) +theme_bw() +theme(panel.grid=element_blank()) + ## 去掉白色外框theme(axis.text=element_blank()) + ## 把图旁边的标签去掉theme(axis.ticks=element_blank()) + ## 去掉左上角的坐标刻度线theme(panel.border=element_blank()) + ## 去掉最外层的正方形边框geom_text(aes(x = 3.5, y = ((ymin+ymax)/2), label = type))
ggplot2--饼状图相关推荐
- chartcontrol饼状图属性设置_温故而知新,ggplot2 饼图的几点笔记
其实 ggplot2 并没有类似于 geom_pie() 这样的函数实现饼图的绘制,它是由 geom_bar() 柱状图经过 coord_polar() 极坐标弯曲从而得到的. 对于为什么 ggp ...
- 安卓饼状图设置软件_话单及银行卡交易智能分析软件
一.产品概况: 思迈奥SMILE数据智能分析软件是由我司自主设计与研发的一款结合在公安和检察院的侦查业务经验而定制研发的数据智能分析系统,包含于话单.电子银行账单.及其它数据(个人出行数据.社会资源数 ...
- android饼状图简书,Charts-饼状图
上篇文章已经讲述了折线图的用法这边文章主要来谈饼状图. 其实Charts难的部分主要在于配置,所以同样主要说说他的配置. pieGraphView.setExtraOffsets(left: 10, ...
- echart饼状图没有数据的时候显示暂无数据_Python数据结构可视化 day 5
Python 数据结构可视化 (Day5) 01年度工作总结 有时候画布太大,影响到图表的展示,这个时候输入: "init_opts=opts.InitOpts(width='',heigh ...
- echart的关系图高亮_echart中饼状图的高亮显示。
1 2 3 4 5 6 7 8 9 10 #main{ 11 width:50vw;height:60vh;margin-left:2vw 12 } 13 14 15 16 17 18 19 20 / ...
- C#应用NPOI实现导出EXcel表格中插入饼状图(可实现动态数据生成)
一.思路: 1.excel是可以通过NPOI插入图片的: 2.C#通过NPOI生成饼状图: 3.把生成的饼状图以字节流的形式插入到表格 二.看代码: #region 生成饼图图例/// <s ...
- mysql 统计做饼状图_PHP+mysql+Highcharts实现饼状统计图
Mysql 首先我们建一张・chart_pie・表作为统计数据. -- edit http://www.lai18.com -- 表的结构 `chart_pie` -- CREATE TABLE IF ...
- .net之生成图表的控件(柱状图,曲线图,饼状图) [转]
可以生成柱状图,曲线图,饼状图,只要你给他一个datatable,在这声明一下 using System; using System.Web.UI; using System.Data; using ...
- Iocomp控件教程之Pie Chart——饼状图控件
Pie Chart--饼状图控件(Pie Chart)以饼状图形式显示每一个项目内容所占的百分比比重.在设计时.能够使用属性编辑器加入或者移除项目以及更改属性值.在执行时.使用AddItem,Remo ...
- js实现反恐精英+曲线图+饼状图
js 画出几十种矢量图 raphaeljs 超炫丽的动画效果 js矢量图,你见过这么绚丽的效果吗? 1.旋转图片动画效果 ---- js 画图 raphaeljs http://raphaeljs. ...
最新文章
- LVM逻辑卷的缩减与删除,LVM逻辑卷快照,btrfs文件系统,网络管理
- String.slice
- NFS部署及优化(一)
- 再谈正态分布或高斯函数
- php $_server[remote_addr];,php – 如何伪造$_SERVER [‘REMOTE_ADDR’]变量?
- 2021年8月下旬好文收藏
- 关于 IE 模态对话框的两个问题
- 基于注解的 Spring MVC(上)
- c c++函数资源释放时避免goto的方法
- QwebSocket即时通信
- 安卓车机没有ADB调试,任意安装第三方软件教程
- 数据中心“容灾”和“备份”的区别
- ubuntu更改网卡设置等出现输入default keyring密码的解决方法
- excel 删除重复项_在Excel 2007中删除重复项
- MAC OS升级记录
- CondConv: Conditionally Parameterized Convolutions for Efficient Inference论文解读
- 运用java打印出菱形
- 运维真的不是夕阳产业!(不谈技术)
- php创建数组教程,PHP 数组
- 中国不可能有金融危机而只有经济危机