R -- 从饼图中删除刻度

标签 r plot

我想从在 R 中的基本图形中创建的饼图中删除刻度线。

pie(x=rep(1, 12), labels=c("Jan", "Feb", "Mar", "Apr",
                       "May", "Jun", "Jul", "Aug",
                       "Sep", "Oct", "Nov", "Dec"),
col = rainbow(12),
border=NA) 

pie 函数似乎不接受 par() 中可以执行此操作的任何图形参数。

最佳答案

您将不得不“破解”该功能。类型 pie查看源代码。然后删除绘制刻度线 lines 的线:

pie_ <- function (x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE, 
                  init.angle = if (clockwise) 90 else 0, density = NULL, angle = 45, 
                  col = NULL, border = NULL, 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
  dev.hold()
  on.exit(dev.flush())
  plot.window(xlim, ylim, "", asp = 1)
  if (is.null(col)) 
    col <- if (is.null(density)) 
      c("white", "lightblue", "mistyrose", "lightcyan", 
        "lavender", "cornsilk")
  else par("fg")
  if (!is.null(col)) 
    col <- rep_len(col, nx)
  if (!is.null(border)) 
    border <- rep_len(border, nx)
  if (!is.null(lty)) 
    lty <- rep_len(lty, nx)
  angle <- rep(angle, nx)
  if (!is.null(density)) 
    density <- rep_len(density, nx)
  twopi <- if (clockwise) 
    -2 * pi
  else 2 * pi
  t2xy <- function(t) {
    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))
    polygon(c(P$x, 0), c(P$y, 0), density = density[i], angle = angle[i], 
            border = border[i], col = col[i], lty = lty[i])
    P <- t2xy(mean(x[i + 0:1]))
    lab <- as.character(labels[i])
    if (!is.na(lab) && nzchar(lab)) {
      #lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y)
      text(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, 
           adj = ifelse(P$x < 0, 1, 0), ...)
    }
  }
  title(main = main, ...)
  invisible(NULL)
}

关于R -- 从饼图中删除刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153159/

相关文章:

r - 使用 AND 或 OR 折叠 bool 向量

r - 总结一个矩阵。获取每 100000 个单位类别的平均值

csv - gnuplot 中 CSV 数据的双倍刻度

matlab - 散点饼图

在 R 中重新格式化 Excel 工作表

在 R 函数中返回多个对象

r - 为什么data.table,设置0x​​104567910 = F,j为单列时输出一个data.table?

matlab - 如何在 Matlab 中将密度信息添加到 2D 图中?

python - Matplotlib 线条连接不顺畅,Python

python - 在 matplotlib 中创建堆叠柱面条形图