【问题标题】:Fill point markers in ggplot2 figure depending on variable根据变量填充ggplot2图中的点标记
【发布时间】:2018-02-01 11:42:43
【问题描述】:

我正在尝试对两个国家/地区的以下数据进行可视化:

library(tidyverse)

df <- tribble(
  ~year, ~country, ~value, ~choice,
  2001, "France", 55, TRUE,
  2002, "France", 53, TRUE,
  2003, "France", 31, FALSE,
  2004, "France", 10, FALSE,
  2005, "France", 30, FALSE,
  2006, "France", 37, FALSE,
  2007, "France", 54, FALSE,
  2008, "France", 58, FALSE,
  2009, "France", 50, TRUE,
  2010, "France", 40, TRUE,
  2011, "France", 49, TRUE,
  2001, "USA", 55, TRUE,
  2002, "USA", 53, TRUE,
  2003, "USA", 64, FALSE,
  2004, "USA", 40, FALSE,
  2005, "USA", 30, FALSE,
  2006, "USA", 39, FALSE,
  2007, "USA", 55, FALSE,
  2008, "USA", 53, TRUE,
  2009, "USA", 71, TRUE,
  2010, "USA", 44, TRUE,
  2011, "USA", 40, TRUE
)

如果choice 列是TRUE,我想填写观察标记。 (在我的真实数据集中,这将对应于重要的值。)

但不知何故,fill 命令没有响应:

ggplot(df, aes(year, value, color = country)) +
  geom_line() +
  geom_point(aes(shape = country, fill = choice), size = 4) +
  theme_minimal() +
  scale_shape_manual(values = c(1, 2))

产生:

【问题讨论】:

    标签: r plot ggplot2 data-visualization


    【解决方案1】:

    按照您的方式,我将执行以下操作。我选择了可​​以操纵填充的绘图符号(即 21 和 24)。我将蓝色分配给 TRUE,将白色分配给 FALSE。

    ggplot(df, aes(x = year, y = value)) +
    geom_line(aes(color = country)) +
    geom_point(aes(shape = country, fill = choice), size = 4, alpha = 0.4) +
    theme_minimal() +
    scale_shape_manual(values = c(21, 24)) +
    scale_fill_manual(values = c("blue", "white"))
    

    鉴于 OP 的评论,这是另一个想法。

    ggplot(df, aes(x = year, y = value)) +
    geom_line(aes(color = country)) +
    geom_point(aes(shape = interaction(country, choice), fill = interaction(country,choice)), size = 4, alpha = 0.4) +
    theme_minimal() +
    scale_shape_manual(values = c(21, 24, 21, 24)) +
    scale_fill_manual(values = c("#F8766D", "#00BFC4", "white", "white"))
    

    【讨论】:

    • 难道不能让填充颜色和线条颜色一样吗?所以对于红线,点会得到红色填充(蓝色三角形也是如此)。
    • @ulima2_ 如果你愿意,让我修改一下我的想法。一分钟。
    • 在下面查看我的答案。
    • @ulima2_ 你的想法基本一样。您使用paste() 创建四个组。您也可以使用interaction() 实现相同的效果。
    【解决方案2】:

    尝试不同的形状。

    # Using OP's data    
    wantedShapes <- c(21, 24)
    library(ggplot2)
    ggplot(df, aes(year, value, color = country)) +
      geom_line() +
      geom_point(aes(shape = country, fill = choice), size = 4) +
      theme_minimal() +
      scale_shape_manual(values = wantedShapes)
    

    您可以在here 找到更多关于形状的信息。您尝试使用的那些没有fill 状态。

    但是,我只将country 用于color 和shape 用于choice 和fill(+ 最少的视觉调整):

    ggplot(df, aes(year, value)) +
        geom_line(size = 1, aes(color = country)) +
        geom_point(aes(shape = choice, fill = choice), 
                   size = 4, alpha = 0.6) +
        scale_shape_manual(values = wantedShapes) +
        scale_fill_brewer(palette = "Set1") +
        scale_color_brewer(palette = "Accent") +
        theme_minimal()
    

    【讨论】:

      【解决方案3】:

      使用线型可能更有助于清楚地传达信息:

      ggplot(ddf, aes(year, value, color=country, linetype=choice))+ 
          geom_point()+ 
          geom_line()+ 
          theme_bw()
      

      【讨论】:

        【解决方案4】:

        所有很好的答案,但基于 @PoGibas 和 @jazzurro 的答案,我想出了最接近我最初想要的答案:

        df <- df %>% 
          mutate(types = paste(country, choice))
        
        ggplot(df, aes(x = year, y = value, color = country)) +
          geom_line() +
          geom_point(aes(shape = types), size = 4, alpha = 0.6) +
          theme_minimal() +
          scale_shape_manual(values = c(16, 1, 17, 2))
        

        我在这里使用的技巧是创建结合country 和choice 的新变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-23
          • 2012-12-17
          • 1970-01-01
          • 1970-01-01
          • 2019-06-24
          • 2013-09-19
          相关资源
          最近更新 更多