微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能使这些样条的颜色变暗?

如何解决我怎样才能使这些样条的颜色变暗?

随着数据点数量增加,样条曲线在背压下变得不可见。 如何使样条线的颜色变暗? 或者用其他方法来纠正这个可见性问题?

  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,scale=1.0,verbose=TRUE) %>%
      mutate(x = x1 * 20) %>%
      rename("Season" = x0) %>%
      mutate(Season = ifelse(Season == "3","Winter",Season == "Winter") %>%
      select(y,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

解决方法

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

您不能使拟合线变暗(除非您想切换到 scale_color_manual(values = c("red","blue")) 之类的东西),但是如果您想更清楚地看到置信度带,您可以增加 alpha 和/或为它们设置 fill 颜色。

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

plot with splines and points

,

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

library(ggplot2)

Plot <- DF %>%
      ggplot(.,colour = Season)) +
      geom_jitter(alpha = 0.2) +
      geom_smooth(method = "gam",lwd = 1.6)

Plot

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。