Reputation: 469
I am struggling to make a graph with double y axis. It comes out without confidence intervals with loess
and I am not able to understand the reason.
Below I am reporting the code:
library(ggplot2)
library(readxl)
Dati <- data.frame("r" = c(0.99, 1.42, 2.10, 3.32, 6.09), "Vix" = c(16500, 19200, 22500, 24000, 26000), "OT" = c(23.5, 19, 11, 9, 7), "ref" = c("PU 178", "PU 178", "PU 178", "PU 178", "PU 178"))
attach(Dati)
scaleFactor <- max(Vix) / max(OT)
Graph <- ggplot(Dati, aes(x= r)) +
geom_point(aes(y= Vix, col=paste0("Vix ", ref)), shape = 1, size = 3.5) +
geom_smooth(aes(y= Vix, col = paste0("Vix ", ref)), method="loess", level=0.55, se = TRUE) +
geom_point(aes(y= OT * scaleFactor, col=paste0("OT ", ref)), shape = 1, size = 3.5) +
geom_smooth(aes(y=OT * scaleFactor, col = paste0("OT ", ref)), method="loess", level=0.55, se = TRUE) +
scale_color_manual(values=c('#644196', '#f92410', '#bba6d9', '#fca49c'),
name = "") +
theme(legend.justification = "top") +
scale_y_continuous(name="Viscosity at 10rpm (mPa s)", sec.axis=sec_axis(~./scaleFactor, name="open time (sec)")) +
theme(
axis.title.y.left=element_text(color='#f92410'),
axis.text.y.left=element_text(color='#f92410'),
axis.title.y.right=element_text(color='#644196'),
axis.text.y.right=element_text(color='#644196'),
legend.position = "none"
) +
scale_x_continuous(name="ratio A2333/AD5027")
Graph
And the result is completely without CI for both lines. I thought it was too big or small the specified level
but also changing it I get no CIs. I thought 5 values are too less to achieve, but I made in the past graph with 5 values without problems.
Does somebody know if I made any mistake?
Below I post the graph which I obtain.
Do
Upvotes: 1
Views: 946
Reputation: 46978
Your span is too small (see this), so there's too little points to estimate your confidence interval. So for example if you do:
ggplot(Dati, aes(x= r)) +
geom_point(aes(y= Vix, col=paste0("Vix ",ref)),shape = 1, size = 3.5) +
geom_smooth(aes(y= Vix, col =paste0("Vix ",ref)), method="loess" ,span=1) +
geom_point(aes(y= OT * scaleFactor, col=paste0("OT ",ref)), shape = 1, size = 3.5) +
geom_smooth(aes(y=OT * scaleFactor, col =paste0("OT ",ref) ), method="loess",span=1) +
scale_color_manual(values=c('#644196', '#f92410', '#bba6d9', '#fca49c'),
name = "") +
theme(legend.justification = "top")
Loess is a bit of an overkill here, you can consider other smooth and also pivoting your data long to make it easier to code:
library(tidyr)
library(dplyr)
Dati %>% mutate(OT = OT*scaleFactor) %>%
pivot_longer(-c(r,ref)) %>%
mutate(name = paste0(name,ref)) %>%
ggplot(aes(x = r,y = value,col = name,fill = name)) +
geom_point(shape = 1, size = 3.5) +
geom_smooth(method="gam",formula = y ~ s(x,k=3),alpha=0.1) +
theme_bw()
Or polynomial of degree 2:
Dati %>% mutate(OT = OT*scaleFactor) %>%
pivot_longer(-c(r,ref)) %>%
mutate(name = paste0(name,ref)) %>%
ggplot(aes(x = r,y = value,col = name,fill = name)) +
geom_point(shape = 1, size = 3.5) +
geom_smooth(method="lm",formula = y ~ poly(x, 2),alpha=0.1) +
theme_bw()
Upvotes: 1