首页 文章

如何在ggplot中平滑具有不同x坐标的图?

提问于
浏览
0

我想用ggplot绘制算法随时间的运行,但我想在一行中平滑它们并添加这些漂亮的色带 . 就像这里(来自ggplot手册的格子和ggplot2,我找不到代码):
enter image description here

但!我有100次运行,全部由1000个数据点组成 . 问题是这些数据点都有不同的x坐标 . 所以我认为平均线不能很好地计算得到这条平滑线?真的吗?

如果是这样,我想绘制100条平滑的线条,并以固定的间隔(比如每个x = 100,x = 200等)对它们进行采样,然后用这些条带生成平均线条(显示方差或90%间隔或所以) .

一旦我在固定的x坐标处采样数据,我可以这样做:

data
months <- c(1:12)
 High <- c(-6,-2,5,14,21,26,28,27,22,14,4,-3)
Low <- c(-16,-11,-5,2,9,14,17,16,11,4,-4,-12)
Mean <- c(-11,-7,0,8,15,20,23,22,16,9,1,-7)
Prepmm <- c(26.4 ,20.1 ,47.2 ,58.7 ,82.3 ,110.2 ,102.6 ,102.9 ,68.3 ,53.6 ,49.3 ,25.4 )
Prep <- Prepmm * 0.1 # converting to cm
minptemp <- data.frame(months, High, Low, Mean, Prep)



# plot
require(ggplot2)  # need to install ggplot2 
plt <- ggplot(minptemp, aes(x= months))
plt + geom_ribbon(aes(ymin= Low, ymax= High), fill="yellow") + geom_line(aes(y=Mean))+
geom_point(aes(x = months, y = Prep)) + theme_bw( ) # ribbon plus point plot months

得到:
enter image description here

如何对100条平滑线进行采样?这有可能吗?或者还有另一种方式吗?

我的数据看起来像这样:

run 1
x  y
1  100
4  90
7  85
10 80

run 2
x  y
1  150
2  85
10 60

100次运行......

所以不完整,你可以看到:

x1; y1  ; x2 ; y3
1 ; 100 ; 1  ; 150
4 ; 90  ; 2  ; 85
7 ; 85  ; 10 ; 60
10; 80  ;

如果在x = 4时取平均值,是否会考虑第二次运行的值在85到60之间?

1 回答

  • 0

    我决定先用R预处理数据:

    xx <- read.table(text='x1; y1  ; x2 ; y2
    1 ; 100 ; 1  ; 150
    4 ; 90  ; 2  ; 85
    7 ; 85  ; 10 ; 60
    10; 80  ;',sep=';',fill=TRUE,header=TRUE)
    
    dm <- merge(xx[,1:2],xx[,3:4],by=1,all=T)
    dm <- dm[!is.na(dm$x1),]
    dm$y1 <- zoo::na.locf(dm$y1)
    dm$y2 <- zoo::na.locf(dm$y2)
    dm
      x1  y1  y2
    1  1 100 150
    2  2 100  85
    3  4  90  85
    4  7  85  85
    5 10  80  60
    

相关问题