代码之家  ›  专栏  ›  技术社区  ›  tjebo

从坐标轴标签中删除最后一个标签和斜杠字符“/”

  •  0
  • tjebo  · 技术社区  · 5 年前

    这是我有点烦-我如何删除这个不需要的标签,包括 / . 简单地说 c("", my actual labels) 没用,因为 / 仍然存在。

    scale_x_date 可能会有帮助,但这实际上绘制了整个日期范围,并没有给出所需的“按月”数字。

    library(ggplot2)
    # modified from mdeaths data set
    lungdeath <-
      structure(list(deaths = c( 2134, 1863, 1877, 1877, 1492, 1249, 1280, 1131, 1209, 1492, 1621, 1846, 1846
      ), month = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0
      ), year = c( 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974
      )), row.names = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "121"
      ), class = "data.frame")
    
    ggplot(lungdeath) +
      geom_line(aes(month, deaths)) +
      coord_polar() +
      scale_x_continuous(breaks = 0:12,  labels = c("undesired",1:12))
    

    reprex package (2.0.0版)

    2 回复  |  直到 5 年前
        1
  •  2
  •   Nicolás Velasquez    5 年前

    scale_x_continuous()

    ggplot(lungdeath) +
      geom_line(aes(month, deaths)) +
      coord_polar() +
      scale_x_continuous(breaks = c(1:12))
    

    enter image description here

        2
  •  1
  •   teunbrand    5 年前

    可能有点想得太多了,但这似乎是硬编码的 render_fg CoordPolar

    library(ggplot2)
    library(grid)
    
    # Constructor
    alt_coord_polar <- function(...) {
      polar_coord <- coord_polar(...)
      # Very ad-hoc changes to the previous class
      coord <- ggproto(
        NULL, polar_coord,
        render_fg = function(self, panel_params, theme) {
          if (is.null(panel_params$theta.major)) {
            return(element_render(theme, "panel.border"))
          }
          
          theta <- ggplot2:::theta_rescale(self, panel_params$theta.major, 
                                           panel_params)
          labels <- panel_params$theta.labels
          
          theta <- theta[!is.na(theta)]
          ends_apart <- (theta[length(theta)] - theta[1]) %% (2*pi)
          if (length(theta) > 0 && ends_apart < 0.05 && !is.null(labels)) {
            n <- length(labels)
            if (is.expression(labels)) {
              combined <- substitute(paste(a, " ", b), # <- changed middle
                                     list(a = labels[[1]], b = labels[[n]]))
            } else {
              combined <- paste(labels[1], labels[n], sep = " ") # <- changed sep
            }
            labels[[n]] <- combined
            labels <- labels[-1]
            theta <- theta[-1]
          }
          
          grobTree(
            if (length(labels) > 0) element_render(
              theme, "axis.text.x",
              labels,
              unit(0.45 * sin(theta) + 0.5, "native"),
              unit(0.45 * cos(theta) + 0.5, "native"),
              hjust = 0.5, vjust = 0.5
            ),
            element_render(theme, "panel.border")
          )
        }
      )
    }
    

    然后使用您自己的自定义极坐标:

    lungdeath <-
      structure(list(deaths = c( 2134, 1863, 1877, 1877, 1492, 1249, 1280, 1131, 1209, 1492, 1621, 1846, 1846
      ), month = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0
      ), year = c( 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974
      )), row.names = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "121"
      ), class = "data.frame")
    
    ggplot(lungdeath) +
      geom_line(aes(month, deaths)) +
      alt_coord_polar() +
      scale_x_continuous(breaks = 0:12,  labels = c("undesired",1:12))
    

    于2021-04-21由 reprex package

    推荐文章