首页 > 解决方案 > 如何在与我的散点图中的点不同的颜色 brewer 调色板中使用回归线?

问题描述

使用 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”作为线条。它会使线条脱颖而出。

我该怎么做?

标签: rggplot2colorscolorbrewer

解决方案


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

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()

结果:

在此处输入图像描述


推荐阅读