繁体   English   中英

R - 从饼图中删除刻度

[英]R — remove ticks from pie chart

我想从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) 

派对函数似乎不接受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)
}

也许不是最好的解决方案,但你可以用ggplot2而不是pie()函数来做你的饼图。

library(ggplot2)

df <- data.frame(y=rep(1, 12),
                 labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

ggplot(df, aes(factor(0), y, fill=factor(labels))) +
    geom_bar(stat="identity") +
    coord_polar(theta="y", start=-pi/2, direction=-1) +
    theme_bw() +
    theme(axis.ticks=element_blank(),
          axis.text.y=element_blank(),
          axis.text.x=element_text(color="black"),
          axis.title=element_blank(),
          legend.position="none",
          panel.grid.major=element_blank(),
          panel.grid.minor=element_blank(),
          panel.border=element_blank()) +
    scale_y_continuous(breaks=cumsum(df$y)-df$y/2,
                       labels=df$labels) +
    scale_fill_manual(values=rainbow(12))

不知何故偶然发现了这个老帖子并想到了另一个,尽管是“hacky”解决方案。 只需在原件上叠加稍大的副本(无标签)即可覆盖刻度线。

par(new = T)
pie(x=rep(1, 12), labels = "",
col = rainbow(12),
radius = .84,
border=NA)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM