I have a data set which X values are integers from 1 to several thousandth and want to plot the mean Y and a measure of dispersion around that mean. The problem I have is that there are some missing X values. Therefore, when using the geom_line and geom_ribbon functions the plot is continuous and I can not find a way to make it leave blanks where there is no data.
Here is a mock up reproducible example.
data.1 <-read.csv(text = "
Treatment, X, Y_value
A,1,120.5
B,1,123.6
C,1,100.4
A,2,120.9
B,2,123.9
C,2,101.0
A,3,122.3
B,3,126.6
C,3,102.3
A,6,124.8
B,6,128.0
C,6,105.5
A,7,129.5
B,7,129.4
C,7,108.9
A,8,132.9
B,8,130.6
C,8,113.9
A,9,137.6
B,9,136.0
C,9,115.3
A,10,138.4
B,10,139.6
C,10,118.9
A,11,143.9
B,11,145.9
C,11,126.6
")
data.1 <- data.1 %>% group_by(X) %>% summarise(mean.y = mean(Y_value),
sd.y = sd(Y_value))
library(ggplot2)
ggplot(data.1, aes(X, mean.y)) +
geom_line(color="red") +
geom_ribbon(aes(ymin=mean.y-sd.y, ymax=mean.y+sd.y), alpha=0.4) +
scale_x_continuous(limits=c(0,11), breaks = c(seq(min(0),max(11), length.out = 12)))+
theme_bw() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank())
Here is the output I am getting:
And this is what I would like to get:
Any hint on how to accomplish this would be really appreciated.
Thanks