zoukankan      html  css  js  c++  java
  • 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

    ggplot2

    如何在图中加百分比:

    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

    饼图

    版权声明:转载请注明出处,谢谢!

  • 相关阅读:
    HTML5的结构
    关于本Blog
    Luogu2568 GCD
    CH5102 Mobile Service
    Luogu3146 [USACO16OPEN]248
    Mobile Service
    23、Echarts拓扑图、D3拓扑图
    22、startAngle: 85-88、ECharts之仪表盘、仪表盘镂空且导入外来图片、品牌车、不等距折线图、图片右侧空白再出图片、心电图、多数据单环、单数据单环、单数据双环、饼图效果、经纬度、10年经济指标(无局部放大)
    21、angular1之分页组件(包含在复杂弹窗组件里,分页组件包含勾选、过滤、拖拽、翻页记忆、请求服务、转圈服务、简单弹窗组件、插槽ng-transclude)
    19、angular1全局方法、五种服务类型、过滤、重要指令(ng-)、单选框|复选框|下拉框三者联合案例展示、下拉表格嵌套、子组件向父组件传值、directive自定义标签、获取不到新value、[].forEach|$.each|angular.forEach用法示例、undefined+1、ui.router路由模块、ui.router实际执行步骤、jqLite的API参考、前端路由、类
  • 原文地址:https://www.cnblogs.com/awishfullyway/p/6505268.html
Copyright © 2011-2022 走看看