rggplot2gamviridis

Plotting geom_smooth with log10 transformation


This is my first post so I am sorry if I did something wrong (hope not!). I am struggling with aesthetics of my GAM plots, tried different options, searched for solutions but did not succeeded.

I am trying to plot GAM results in log-log space (both x and y axis need to be log10 transformed). A want to use viridis pallete for lines and confidence intervals. I would like my plot had line and confidence intervals look like in this one:
enter image description here

Here is code that generated data that I used to fit GAMs and create plots:

# libraries
library(dplyr)
library(mgcv)
library(tidymv)
library(ggplot2)
library(viridis)

# create sample data

area <- c(1,1,1,1,1,1,1,1,1,1,10,10,10,10,10,10,10,10,10,10,100,100,100,100,100,100,100,100,100,100)
value.1 <- c(2,3,2,2,2,2,2,3,2,2,9,11,14,12,10,12,11,10,15,10,28,27,26,28,26,27,29,28,27,29)
value.2 <- c(5,8,5,6,4,5,6,6,6,2,10,11,12,16,14,11,10,11,13,12,14,19,20,21,16,14,17,21,18,21)

data.class.1 <- cbind.data.frame(area,value.1)
data.class.2 <- cbind.data.frame(area, value.2)

# fitting model
mod.1 <- gam(value.1 ~ s(log10(area), k=3), data = data.class.1)
mod.1  
summary(mod.1)  

mod.2 <- gam(value.2 ~ s(log10(area), k=3), data = data.class.2)
mod.2  
summary(mod.2)  

# get preicted values
pred.class.1 <- predict_gam(mod.1) %>%
  mutate(class = "Class 1") 
  
pred.class.2 <- predict_gam(mod.2) %>%
  mutate(class = "Class 2") 

# combine predicted values from both models to be able to plot them on one plot

dataset <- rbind(pred.class.1, pred.class.2)

I ploted my data using tidymv::geom_smooth_ci() with scale_color_viridis(discrete = T)

ggplot(dataset, aes(area, fit)) +
  geom_smooth_ci(class) +
  theme_minimal() +
  scale_y_continuous(trans = "log10", breaks = c(2,4,8,16)) +
  scale_x_log10()+
  scale_color_viridis(discrete = T) 

The plot looks great but only lines have right colours. I have no idea hot to modify colours of confidence intervals (I tried whatever I could):
enter image description here

Than I thought that maybe I can get what I need just with geom_smooth. I was very close, which means that I had a nice plot:

enter image description here

So the only last thing to do was x axis transformation. I just added scale_x_log10 as in the code below:

ggplot(dataset, aes(area, fit)) +
  geom_smooth(
    aes(area, fit, colour = class, fill = class),
    se = T)+
  theme_minimal() +
  scale_y_continuous(trans = "log10", breaks = c(2,4,8,16)) +
  scale_x_log10() +
  scale_color_viridis(discrete = TRUE, option = "D")+
  scale_fill_viridis(discrete = TRUE)

With this transformation confidence intervals just dissapeared (in fact, when I zoom in, I can see some little, tiny, narrow confidence intervals).

enter image description here

I would appreciate any ideas on how to solve it!


Solution

  • Below, I provide the code to obtain colored confidence intervals (CIs) using tidymv::geom_smooth_ci().
    I removed scale_color_viridis just to better visualize the CIs.

    ggplot(dataset, aes(x=area, y=fit, color=class, fill=class)) +
      geom_smooth_ci() +
      theme_minimal() +
      scale_y_continuous(trans = "log10", breaks = c(2,4,8,16)) +
      scale_x_log10()
    

    enter image description here

    The same graph can also be obtained without using geom_smooth_ci. The solution proposed below is more flexible and allows for the elimination of the CIs borders.

    ggplot(dataset, aes(x=area, y=fit, color=class, fill=class)) +
      geom_ribbon(aes(ymin = fit - (se.fit * 1.96), 
                      ymax = fit + (se.fit * 1.96)), 
                      alpha = 0.1, color="transparent")+
      geom_path() +
      theme_minimal() +
      scale_y_continuous(trans = "log10", breaks = c(2,4,8,16)) +
      scale_x_log10()
    

    enter image description here