简体   繁体   中英

line plot with 3 standart deviation in ggplot2 in r

I made plot in ggplot2 with maximum and minimum of each point

x=rep(1:10,3)
y_all = c(1:10,2:11,3:12)


data = as.data.frame(cbind(x,y_all))


pic_1 = ggplot(data, aes(x=data$x,y=data$y_all))  
pic_2 = pic_1 + stat_summary(fun.y = mean, geom = 'line', colour = 'blue')
pic_3 = pic_2+stat_summary(fun.y = mean, geom = 'ribbon',fun.ymax = max, fun.ymin =min)
pic_4=pic_3+stat_summary(fun.y = mean, geom = 'line', colour ='red',size=1)

我的结果行有最大值和最小值

Tell me please How I can change my code that can help me to plot 3 standard deviation instead maximum and minimum?

This can be done with two auxiliary functions that compute the ymin and ymax limits.
Also, I have changed the order of the first geom = 'line' call. It was being overplotted by the ribbon.

fun_ymin <- function(y) mean(y) - 3*sd(y)
fun_ymax <- function(y) mean(y) + 3*sd(y)

pic_1 <- ggplot(data, aes(x = x, y = y_all))  
pic_2 <- pic_1 + stat_summary(fun.y = mean, geom = 'ribbon', fun.ymax = fun_ymax, fun.ymin = fun_ymin)
pic_3 <- pic_2 + stat_summary(fun.y = mean, geom = 'line', colour = 'blue', size = 2)
pic_4 <- pic_3 + stat_summary(fun.y = mean, geom = 'line', colour = 'red', size = 1)
pic_4

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM