R语言如何绘制饼图/甜甜圈图(15)

1.什么是饼图/甜甜圈图?

在工作中如果遇到需要计算总费用或金额的各个部分构成比例的情况,一般都是通过各个部分与总额相除来计算,而且这种比例表示方法单看数字很抽象,我们可以使用一种饼形图表,能够直接以图形的方式直接显示各个组成部分所占比例,更加形象直观。

饼图,是指使用圆形及圆内扇形的面积来表示数值大小的图形,其一般用于表示总体中各部分所占的比例。

甜甜圈图(圆环图),其本质是将饼图的中间区域挖空。虽然如此,甜甜圈图还是有其优点的。饼图的整体性太强,会让我们将注意力集中在比较饼图内各个扇形之间占整体比重的关系。但如果我们将两个饼图放在一起,则很难同时对比两个图。甜甜圈图在解决上述问题时,采用了让我们更关注长度而不是面积的做法。这样我们就能相对简单地对比不同的甜甜圈图。

本文我们就来讨论一下饼图/甜甜圈图是如何绘制的以及如何对其进行解读。

2.绘图前的数据准备

​ demo数据可以在https://www.bioladder.cn/shiny/zyp/bioladder2/demoData/Pie/data.csv下载。

包含2列数据,第一列是名称,第二列是数值。

在这里插入图片描述

3. R语言怎么画饼图/甜甜圈图

3.1 饼图

# 加载R包,没有安装请先安装  install.packages("包名") 
library(graphics)

# 读取饼图/甜甜圈图数据文件
df= read.delim("https://www.bioladder.cn/shiny/zyp/bioladder2/demoData/Pie/data.csv",# 这里读取了网络上的demo数据,将此处换成你自己电脑里的文件
               sep = "," # 因为文件为csv格式,所以分隔符设置为","逗号
)   

# 设置标签样式(名称+百分比+原数值)
labs <- paste0(df$group," \n(", round(df$value/sum(df$value)*100,2), "%)"," \n(",df$value, ")")

# 绘图
pie(df$value,
    labels=labs, 
    init.angle=90,    # 设置初始角度
    col = rainbow(length(df$value)) , # 设置颜色 
    border="white",   # 边框颜色 
    cex = 1)          # 字体大小

3.2 甜甜圈图

# 并没有直接画甜甜圈图的R包,所以在饼图源代码的基础上改改
doughnut <- function (x, labels = names(x), edges = 200, outer.radius = 0.8,
                      inner.radius=0.6, clockwise = FALSE,
                      init.angle = if (clockwise) 90 else 0, density = NULL,
                      angle = 45, col = NULL, border = FALSE, lty = NULL,
                      main = NULL, ...)
{
  if (!is.numeric(x) || any(is.na(x) | x < 0))
    stop("'x' values must be positive.")
  if (is.null(labels))
    labels <- as.character(seq_along(x))
  else labels <- as.graphicsAnnot(labels)
  x <- c(0, cumsum(x)/sum(x))
  dx <- diff(x)
  nx <- length(dx)
  plot.new()
  pin <- par("pin")
  xlim <- ylim <- c(-1, 1)
  if (pin[1L] > pin[2L])
    xlim <- (pin[1L]/pin[2L]) * xlim
  else ylim <- (pin[2L]/pin[1L]) * ylim
  plot.window(xlim, ylim, "", asp = 1)
  if (is.null(col))
    col <- if (is.null(density))
      palette()
  else par("fg")
  col <- rep(col, length.out = nx)
  border <- rep(border, length.out = nx)
  lty <- rep(lty, length.out = nx)
  angle <- rep(angle, length.out = nx)
  density <- rep(density, length.out = nx)
  twopi <- if (clockwise)
    -2 * pi
  else 2 * pi
  t2xy <- function(t, radius) {
    t2p <- twopi * t + init.angle * pi/180
    list(x = radius * cos(t2p),
         y = radius * sin(t2p))
  }
  for (i in 1L:nx) {
    n <- max(2, floor(edges * dx[i]))
    P <- t2xy(seq.int(x[i], x[i + 1], length.out = n),
              outer.radius)
    polygon(c(P$x, 0), c(P$y, 0), density = density[i],
            angle = angle[i], border = border[i],
            col = col[i], lty = lty[i])
    Pout <- t2xy(mean(x[i + 0:1]), outer.radius)
    lab <- as.character(labels[i])
    if (!is.na(lab) && nzchar(lab)) {
      lines(c(1, 1.05) * Pout$x, c(1, 1.05) * Pout$y)
      text(1.1 * Pout$x, 1.1 * Pout$y, labels[i],
           xpd = TRUE, adj = ifelse(Pout$x < 0, 1, 0),
           ...)
    }      
    Pin <- t2xy(seq.int(0, 1, length.out = n*nx),
                inner.radius)
    polygon(Pin$x, Pin$y, density = density[i],
            angle = angle[i], border = border[i],
            col = "white", lty = lty[i])
  }
  
  title(main = main, ...)
  invisible(NULL)
}

# 绘图
doughnut(
  df$value,
  labels=labs, 
  init.angle=90,     # 设置初始角度
  col = rainbow(length(df$value)) , # 设置颜色 
  border="white",    # 边框颜色 
  inner.radius= 0.4, # 内环大小
  cex = 1)           # 字体大小

4. BioLadder生信云平台在线绘制饼图/甜甜圈图

不想写代码?可以用BioLadder生信云平台在线绘制饼图/甜甜圈图。

饼图网址:https://www.bioladder.cn/web/#/chart/38

甜甜圈图网址:https://www.bioladder.cn/web/#/chart/39

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-evG7Xu6k-1652085241036)(D:\gitee\bioladder2\文档\知乎\饼图、甜甜圈图.assets\image-20220104140123160.png)]

5. 饼图/甜甜圈图结果解读

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LOfS4hHs-1652085241037)(D:\gitee\bioladder2\文档\知乎\饼图、甜甜圈图.assets\image-20220104142127729.png)]

饼图/甜甜圈图都是用比例显示数值大小的图形。数值越高,该组别所占的比例。

甜甜圈图本质是将饼图的中间区域挖空

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 可以使用 Apache POI 库来操作 Excel 文件,其中也包括创建和环形。 以下是一个简单的示例代码,可以创建一个包含的 Excel 文件: ``` import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.*; public class PieChartExample { public static void main(String[] args) throws Exception { // 创建 Excel 文件 XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Pie Chart"); // 创建数据行 Row row; Cell cell; row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("Category"); cell = row.createCell(1); cell.setCellValue("Value"); row = sheet.createRow(1); cell = row.createCell(0); cell.setCellValue("Apples"); cell = row.createCell(1); cell.setCellValue(20); row = sheet.createRow(2); cell = row.createCell(0); cell.setCellValue("Oranges"); cell = row.createCell(1); cell.setCellValue(30); row = sheet.createRow(3); cell = row.createCell(0); cell.setCellValue("Pears"); cell = row.createCell(1); cell.setCellValue(10); row = sheet.createRow(4); cell = row.createCell(0); cell.setCellValue("Bananas"); cell = row.createCell(1); cell.setCellValue(40); // 创建 XSSFDrawing drawing = sheet.createDrawingPatriarch(); XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 1, 15, 15); XSSFChart chart = drawing.createChart(anchor); chart.setTitleText("Fruit Sales"); chart.setTitleOverlay(false); XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(org.apache.poi.xddf.usermodel.chart.LegendPosition.TOP_RIGHT); XDDFDataSource<String> categories = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 4, 0, 0)); XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 4, 1, 1)); XDDFChartData data = chart.createData(ChartTypes.PIE, null, null); data.setVaryColors(true); data.addSeries(categories, values); chart.plot(data); // 保存文件 FileOutputStream fileOut = new FileOutputStream("PieChartExample.xlsx"); workbook.write(fileOut); fileOut.close(); workbook.close(); } } ``` 这个示例代码创建了一个 Excel 文件,包含一个名为 "Pie Chart" 的工作表,该工作表包含一个简单的数据表格和一个。你可以根据需要修改数据和表的设置来满足你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值