【问题标题】:Several colors for the same tick/label同一个刻度/标签的几种颜色
【发布时间】:2018-12-21 10:42:24
【问题描述】:

我的数据:

dat <- data.frame(x = c(1,2,3,4,5,6), y = c(2,3,4,6,2,3))

我的情节的中断和标签:

breaks <- c(3,5)
labels <- c(paste(3,"(0.3)"), paste(5,"(0.5)"))

还有我的情节:

library(ggplot2)
ggplot() + 
  geom_point(data = dat, aes(x = x, y = y)) + 
  scale_y_continuous(breaks = breaks, labels = labels)

我希望为相同的标签涂上不同的颜色。例如,我希望用不同于“(0.3)”的颜色为“3”着色。

【问题讨论】:

    标签: r ggplot2 axis-labels


    【解决方案1】:

    这是一种将 2 个地块与 patchwork 粘贴在一起的方法,这是一个类似于 cowplot 的包,但具有更多的灵活性。我将标签分成 2 个向量,一个带有整数,一个带有括号中的小数。然后制作 2 个图,一个用于没有其他标记的外部标签,一个用于主图。

    在做了一轮尝试构建这个之后,我开始调整每个主题的边距,意识到我需要将顶部和底部边距设置为相同,但在左侧图的右侧和左侧没有边距右侧情节的一侧,因此它们之间的空间很小。肯定还有办法对此进行调整,但我会从一些间距开始。

    library(tidyverse)
    library(patchwork)
    
    lbl_int <- str_extract(labels, "^\\d+")
    lbl_frac <- str_extract(labels, "\\(.+\\)")
    

    主要情节相当简单,只是从主题左侧删除了元素。

    (main_plot <- ggplot(dat, aes(x = x, y = y)) + 
      geom_point() + 
      scale_y_continuous(breaks = breaks, labels = lbl_frac) +
      theme(axis.text.y.left = element_text(color = "gray"), 
            axis.title.y.left = element_blank(),
            plot.margin = margin(1, 1, 1, 0, "mm")))
    

    外部标签的绘图已删除大部分主题元素,但有 y 轴标题和标签。

    (int_plot <- ggplot(dat, aes(x = 0, y = y)) + 
      scale_y_continuous(breaks = breaks, labels = lbl_int) +
      theme(axis.text.y.left = element_text(color = "black"), 
            axis.title.y.left = element_text(color = "black"),
            axis.title.x = element_blank(),
            panel.grid = element_blank(),
            axis.ticks = element_blank(),
            axis.text.x = element_blank(),
            panel.background = element_blank(),
            plot.margin = margin(1, 0, 1, 1, "mm")))
    

    然后patchwork 可以很容易地将绘图添加到一起(字面意思是+),然后设置宽度。同样,您可以根据需要调整以下内容,但与右侧相比,我使左侧情节非常瘦。

    int_plot + main_plot +
      plot_layout(ncol = 2, widths = c(1e-3, 1))
    

    reprex package (v0.2.1) 于 2018 年 12 月 21 日创建

    【讨论】:

      【解决方案2】:

      这是让你前进的东西(归功于我改编的 this 答案)。

      我们使用annotate 将您的labels 绘制在两个不同的x 轴坐标上,这将用作我们的标签(因此我们需要关闭theme 中的实际标签)。

      首先,我们创建两个向量,其中包含我们想要的不同颜色的确切标签。

      dat <- data.frame(x = c(1,2,3,4,5,6), y = c(2,3,4,6,2,3))
      
      breaks <- c(3,5)
      labels_new1 <- c(NA, NA, 3, NA, 5, NA) # NA in order to skip that annotation
      labels_new2 <- c(NA, NA, "(0.3)", NA, "(0.5)", NA)
      

      重要部分:

      • coord_cartesian(xlim = c(0, 6), expand = FALSE) + # this will cut our plot properly

      • plot.margin = unit(c(1, 1, 1, 5), "lines") # this will give us some space on the left

      请注意,在coord_cartesian 的定义中,我们实际上切断了两个注释(请注意,您在下一部分中看到的两个 x 值 (-1, -0.5) 超出了xlim 范围)。

      绘图对象:

      g1 <- ggplot() + 
        geom_point(data = dat, aes(x = x, y = y)) + 
        annotate(geom = "text", y = seq_len(nrow(dat)), x = -1, label = labels_new1, size = 4) +
      #first the number add color = "blue" for example
        annotate(geom = "text", y = seq_len(nrow(dat)), x = -0.5, label = labels_new2, size = 4, color = "red") +
      #second the parenthesis (colored in red)
        coord_cartesian(xlim = c(0, 6), expand = FALSE) +
        scale_y_continuous(breaks = breaks) +
      #now lets shut off the labels and give us some left space in the plot 
        theme(plot.margin = unit(c(1, 1, 1, 5), "lines"),
              axis.title.y = element_blank(),
              axis.text.y = element_blank())
      

      最后:

      g2 <- ggplot_gtable(ggplot_build(g1)) # convert to grob
      g2$layout$clip[g2$layout$name == "panel"] <- "off" # clipping of the axes
      # this will show the two annotations that we left off before
      grid::grid.draw(g2)
      

      备注:

      您可以使用x=-1x=-0.5 来移动两个注释,并使用c(1, 1, 1, 5) 中的最后一个值来为左侧留出更多空间。

      labels_new1labels_new2 非常重要,他们正在做所有繁重的工作 whatwhere 你想展示一些东西。

      【讨论】:

        猜你喜欢
        • 2018-10-06
        • 2016-02-19
        • 2011-12-08
        • 1970-01-01
        • 2016-06-14
        • 2020-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多