【问题标题】:How can I darken the colour of these splines?如何使这些样条线的颜色变暗?
【发布时间】:2021-09-26 03:31:28
【问题描述】:

随着数据点数量的增加,样条线在背景下变得不可见。 如何使样条线的颜色变暗? 或者另一种解决此可见性问题的方法?

  library(dplyr)
    library(ggplot2)
    library(mgcv)
    
    # Summer
    Summer <- mgcv::gamSim(eg=5,n=10000,dist="normal",scale=0.6,verbose=TRUE) %>%
      mutate(x = x2 * 20) %>%
      rename("Season" = x0) %>%
      mutate(Season = ifelse(Season == "1", "Summer", Season)) %>%
      filter(.,Season == "Summer") %>%
      select(y, x, Season)
    
    # Winter
    Winter <- mgcv::gamSim(eg=5,n=10000,dist="normal",scale=1.0,verbose=TRUE) %>%
      mutate(x = x1 * 20) %>%
      rename("Season" = x0) %>%
      mutate(Season = ifelse(Season == "3", "Winter", Season)) %>%
      filter(.,Season == "Winter") %>%
      select(y, x, Season)
    
    # Bind
    DF <- rbind(Summer, Winter)
    
    
    # Plot
    Plot <- DF %>%
      ggplot(., aes(x = x, y = y, colour = Season)) +
      geom_jitter() +
      geom_point(shape=21, alpha = 0.5,  size=0.05) +
      geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 10),  lwd = 1.6)
    Plot

【问题讨论】:

    标签: r ggplot2 colors spline


    【解决方案1】:

    如果您希望线条突出于点,那么我会将alpha 调整为geom_jitter。部分问题是您在抖动上绘制点。我认为你不需要这两个(除非我遗漏了什么)。

    library(ggplot2)
    
    Plot <- DF %>%
          ggplot(., aes(x = x, y = y, colour = Season)) +
          geom_jitter(alpha = 0.2) +
          geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 10),  lwd = 1.6)
    
    Plot
    

    【讨论】:

      【解决方案2】:
      1. 您的geom_jitter() 正在创建完整大小和完全不透明的点;相反,将position = "jitter" 添加到geom_point()(我假设您真的不希望两个抖动点和点在其原始位置?)
      2. 我在您的geom_point() 中调整了alphasize,但您可能想要更多地使用它们(即随着样本大小的增加减小大小和不透明度)。如果您的数据集变得非常大,您可以尝试使用 geom_hexbin()
      3. 我发现通过theme_bw()(甚至theme_classic())将背景更改为白色可以更轻松地查看背景中的值。

      您不能使拟合线更暗(除非您想切换到 scale_color_manual(values = c("red", "blue")) 之类的东西),但如果您想更清楚地看到置信带,您可以增加 alpha 和/或设置 @987654334 @color 给他们。

      Plot <- DF %>%
        ggplot(., aes(x = x, y = y, colour = Season)) +
        geom_point(shape=21, alpha = 0.7,  size=0.5, position = "jitter") +
        geom_smooth(method = "gam", formula = y ~ s(x, bs = "cs", k = 10),  lwd = 1.6) + 
        theme_bw()
      

      【讨论】:

        猜你喜欢
        • 2018-12-04
        • 2020-06-09
        • 2016-01-09
        • 2013-01-09
        • 2017-09-20
        • 1970-01-01
        • 2012-01-23
        • 2013-10-07
        • 2017-12-27
        相关资源
        最近更新 更多