简体   繁体   English

r中ggplot2中有3个标准差的线图

[英]line plot with 3 standart deviation in ggplot2 in r

I made plot in ggplot2 with maximum and minimum of each point 我在ggplot2中绘制了每个点的最大值和最小值

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? 请告诉我如何更改我的代码,可以帮助我绘制3个标准差而不是最大值和最小值?

This can be done with two auxiliary functions that compute the ymin and ymax limits. 这可以通过计算yminymax限制的两个辅助函数来完成。
Also, I have changed the order of the first geom = 'line' call. 另外,我已经改变了第一个geom = 'line'调用的顺序。 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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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