How can I darken the colour of these splines?

Viewed 73

As the number of data points grows, the splines become invisible against the back drop. How can I darken the colour of the splines? Or another method to correct this visibility issue?

  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
2 Answers
  1. Your geom_jitter() is creating points of full size and full opacity; instead, add position = "jitter" to geom_point() (I'm assuming you don't really want both jittered points and points in their original positions?)
  2. I adjusted the alpha and size in your geom_point(), but you may want to play with them some more (i.e., decrease size and opacity as the sample size increases). If your data set gets really big you could experiment with geom_hexbin() instead.
  3. I find that changing the background to white via theme_bw() (or even theme_classic()) makes it easier to see the values against the background.

You can't make the fitted lines darker (unless you want to switch to something like scale_color_manual(values = c("red", "blue"))), but if you want to see the confidence ribbons more clearly you could increase alpha and/or set the fill colour for them.

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

If you are wanting the lines to stand out over the points, then I would adjust the alpha for geom_jitter. Part of the issue is that you are plotting points over the jitter. I don't think you need both of these (unless I'm missing something).

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

enter image description here

Related