【问题标题】:ggplot2 - using different types for color scalesggplot2 - 使用不同类型的色标
【发布时间】:2017-03-18 07:41:12
【问题描述】:

我想独立使用色标,即用于连续和分类标度。

我有一个特定的应用程序,我有一个像这里这样的简单情节

mtcars %>%
  mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>%
  ggplot(aes(x = mpg, y = wt)) +
  geom_point(aes(color = drat)) +
  facet_wrap(~ cyl)

但我也想要highlight border, like in the answer of Seth_P, of a specific condition(我不想使用填充背景!)。例如

mtcars %>%
  mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>%
  ggplot(aes(x = mpg, y = wt)) +
  geom_point() +
  geom_rect(xmin = -Inf, xmax = Inf, show.legend = FALSE,
            ymin = -Inf, ymax = Inf, aes(col = cylcol), fill = "#00000000") + 
  facet_wrap(~ cyl)

现在我想“结合”这两者,例如:

mtcars %>%
  mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>%
  ggplot(aes(x = mpg, y = wt)) +
  geom_point(aes(color = drat)) +
  geom_rect(xmin = -Inf, xmax = Inf, show.legend = FALSE,
            ymin = -Inf, ymax = Inf, aes(col = cylcol), fill = "#00000000") + 
  facet_wrap(~ cyl)

这会产生错误Error: Discrete value supplied to continuous scale。一方面这是有道理的,但另一方面,由于两者都使用独立变量,我想使用“不同的色标”。我可以使用覆盖,例如here, where facet colors are plotted over the plot,但我非常感谢一个更简单的解决方案。我知道specifying fill and color separately - 但这不是目标。我真的很想在不同的尺度上使用颜色。有什么建议么 ?

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    我不知道有任何方法可以同时拥有离散和连续 colour 刻度。但是,您可以使用由fill 而不是colour 填充的geom_point 形状来解决它:

    library(ggplot2)
    library(dplyr)
    mtcars %>%
      mutate(cylcol = factor(if_else(cyl == 6, "six", "not six"))) %>%
      ggplot(aes(x = mpg, y = wt)) +
      geom_point(aes(fill = drat), shape = 21) +
      geom_rect(aes(colour = cylcol), xmin = -Inf, xmax = Inf,
        ymin = -Inf, ymax = Inf, fill = NA) +
      facet_wrap(~ cyl)
    

    这是第二种方法,它可能有点过于复杂,但可以通过拆分数据并有效地手动进行分面来工作。宽度需要根据y轴标签大小、图例大小和绘图大小手动调整。

    library(ggplot2)
    library(dplyr)
    library(purrr)
    lims <- list(x = range(mtcars$mpg), y = range(mtcars$wt))
    
    make_plot <- function(data) {
      cyl <- data$cyl[1]
      if (cyl == 6) {
        rec_col <- "light blue"
      } else {
        rec_col <- "red"
      }
      p <- data %>%
        ggplot(aes(x = mpg, y = wt)) +
        geom_point(aes(colour = drat)) +
        geom_rect(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf,
                  fill = NA, colour = rec_col) +
        xlim(lims$x) + ylim(lims$y) +
        facet_wrap(~ cyl)
      if (cyl != 4) {
        p <- p + theme(axis.title.y = element_blank(), axis.ticks.y = element_blank(),
                       axis.text.y = element_blank())
      }
      if (cyl != 8) {
        p <- p + theme(legend.position = "none")
      }
      if (cyl != 6) {
        p <- p + theme(axis.title.x = element_text(colour = "white"))
      }
      p
    }
    
    mtcars %>%
      split(.$cyl) %>%
      map(make_plot) %>%
      grid.arrange(grobs = ., layout_matrix = matrix(1:3, nrow = 1),
                   widths = c(0.32, 0.28, 0.4))
    

    最后,值得注意的是considered three years ago,但哈德利觉得开发带宽不够。

    【讨论】:

    • 谢谢。在帖子中,我已经写过我知道使用填充和颜色美学。但是,在我的实际绘图中(此处未显示),我已经使用填充来获取其他类型的信息。这种解决方法也可以,但对我不起作用。
    • 对不起@Drey。我知道您知道单独的美学,只是人们通常不知道geom_point 的某些形状使用填充而不是颜色作为颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    相关资源
    最近更新 更多