2014-02-19 21 views
0

我想用ggplot繪製算法隨着時間的推移運行,但我想在一行中平滑它們並添加這些漂亮的絲帶。像這裏一樣(ggplot手冊中的lattice和ggplot2,我找不到代碼): enter image description here如何在ggplot中使用不同的x座標平滑繪圖?

但是!我有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之間?

回答

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 
相關問題