【问题标题】:How to have regression lines in a different color brewer pallete than the points in my scatter plot?如何在与我的散点图中的点不同的颜色 brewer 调色板中使用回归线?
【发布时间】:2019-02-17 18:35:32
【问题描述】:

使用 GapMinder 数据,我用不同的大陆回归线绘制了下图:

代码如下:

ggplot(gapminder_82, 
       aes(gdpPercap, lifeExp, color = continent)) + 
  geom_point() + 
  scale_x_log10() +
  scale_color_brewer(palette = "Set2") +
  geom_smooth(method = "lm", se = F)

问题是这些线条并不是真正可见的。所以我想使用来自 color brewer 的 2 种不同的调色板。 Pastel2 表示点,但我想用“Dark2”表示线条。它会使线条突出。

我该怎么做?

【问题讨论】:

    标签: r ggplot2 colors colorbrewer


    【解决方案1】:

    您可以对点使用填充点形状,允许您对点使用填充比例并为线条使用颜色:

    ggplot(gapminder_82, 
           aes(gdpPercap, lifeExp)) + 
        # Make the edge color for the points totally transparent
        geom_point(aes(fill = continent), shape = 21, size = 3, colour = "#FFFFFF00") + 
        scale_x_log10() +
        geom_smooth(aes(color = continent), method = "lm", se = F) +
        scale_fill_brewer(palette = "Pastel2") +
        scale_color_brewer(palette = "Dark2") + 
        theme_bw()
    

    结果:

    【讨论】:

      【解决方案2】:

      即使可以使用单独的调色板,我认为这会导致混淆,因为您会将相同的变量映射到两种不同的颜色。

      如何调整点的 alpha 以增加线条的可见性?

      gapminder_82 %>% 
        ggplot(aes(gdpPercap, lifeExp)) + 
          geom_point(aes(color = continent), alpha = 0.1) + 
          geom_smooth(method = "lm", 
                      se = FALSE, 
                      aes(color = continent)) +
          scale_x_log10() +
          scale_color_brewer(palette = "Set2") +
          theme_bw()
      

      【讨论】:

      • 您的解决方案更简单,我非常喜欢它。我没有将其标记为已接受,因为另一个回答了我的具体问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2018-09-23
      • 2020-05-25
      • 2019-11-03
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多