2013-03-17 95 views
1

我正在可視化一個4維數據集。 讓我們將變量表示爲x, y1, y2y3,其中x用於日期,y是連續變量,而y2, y3是2維矢量的成分(y2, y3)。現在我想要xy1的線圖,另外在(x, y1)處附加了針對(y2, y3)的箭頭。將箭頭附加到點

我已經試過

ggplot(data=data,aes(x=x,y=y1)) + geom_line() + 
     geom_segment(aes(xend=x+y2,yend=y1+y3), arrow = arrow()) 

,但這樣我想我可能需要做一些縮放它不能很好地工作。我怎樣才能做到這一點ggplot

UPDATE:我附上了sample data set(連同它的column definition)。數據集包含來自位於赤道太平洋各處的一系列浮標的海洋學和地面氣象讀數。預計這些數據有助於理解和預測厄爾尼諾/南方濤動(ENSO)週期(來自repository的描述)。現在,例如,我想用上面描述的符號來形象化x=day, y1=humidity, y2=zon.winds, y3=mer.winds

UPDATE2:比如,我想繪製該特定浮標 enter image description here

+0

請提供一些示例數據讓我們能夠幫助您。 – 2013-05-31 08:23:38

+0

@DidzisElferts謝謝,我已經更新了我的問題。 – ziyuang 2013-05-31 10:34:02

回答

2

我有麻煩搞清楚你要顯示的內容。 就我所見,您的數據集有50個浮標,每個浮標每天都會進行一次測量。

library(ggplot2) 
elnino <- read.table('elnino.txt', col.names=c('buoy','day','latitude','longitude','zon.winds','mer.winds','humidity','air.temp','ss.temp'), as.is=TRUE, na='.') 
elnino <- elnino[elnino$humidity > 40,] # removing a single point that seems to be an outlier. 

ggplot(elnino, aes(x=day,y=humidity, group=buoy)) + geom_point() 
ggplot(elnino, aes(x=day,y=humidity, group=buoy)) + geom_line() 

這給出了這兩個結果。 enter image description here enter image description here

我也看不出是你怎麼想顯示的'zon.winds「」和「」 mer.winds'變量?我把這些數字結合起來給出了一個向量,但是你想把它們放在哪裏?你會得到700箭填滿你的情節。

更新 在這種情況下,你這樣做是正確,你必須使用geom_segment並計算「」 X「」,「」 xend的「」,「」 Y「」和「」 YEND',見geom_segment

# We select a single buoy 
el <- subset(elnino, buoy==1) 

library(grid) 
ggplot(el, aes(x=day,y=humidity, group=buoy)) + geom_line() + geom_segment(aes(yend=humidity+zon.winds, xend=day+mer.winds), arrow = arrow(length = unit(0.1,"cm"))) 

然而,這並不是美國能源部看起來非常漂亮,因爲在'zon.winds'座標以及'mer.winds「」被視爲絕對的!所以要利用它們,我們需要對它們進行一些手動轉換。我的價值絕對是任意的。

el <- transform(el, zon.winds = zon.winds * -0.3, mer.winds=mer.winds * -0.3) 

enter image description here

+0

謝謝你的回答!我用一個例子再次更新了我的問題。我認爲這會更清楚。 – ziyuang 2013-05-31 13:27:24