2013-12-13 69 views
2

我想創建一個使用ggplot2的方面圖,代表每月的風向圖。使用ggplot2的方案圖

我最近看了下面的帖子:How to map wind direction and speed (velocity plot) with R。我認爲這對我來說可能是一個好的開始。以下數據集將風值表示爲u和v分量,時間步長爲3小時。我想將它表示爲一個hodogram,這意味着每個矢量都在前一個矢量之後。

   u    v 
[1,] -4.0000000 -6.928203e+00 
[2,] -6.1283555 -5.142301e+00 
[3,] -5.0000000 1.224647e-15 
[4,] -3.7587705 1.368081e+00 
[5,] 4.0000000 -4.898587e-16 
[6,] 4.6984631 -1.710101e+00 
[7,] 5.6381557 2.052121e+00 
[8,] 6.1283555 5.142301e+00 
[9,] -9.1925333 -7.713451e+00 
[10,] -6.5778483 2.394141e+00 
[11,] -5.3623111 4.499513e+00 
[12,] -4.5962667 3.856726e+00 
[13,] -7.0000000 1.714506e-15 
[14,] -6.5778483 -2.394141e+00 
[15,] 6.0000000 -7.347881e-16 
[16,] -6.5778483 -2.394141e+00 
[17,] -6.0000000 1.469576e-15 
[18,] -8.0000000 1.959435e-15 
[19,] -5.6381557 2.052121e+00 
[20,] -6.0000000 1.469576e-15 
[21,] -4.5962667 3.856726e+00 
[22,] 2.0000000 -3.464102e+00 
[23,] 5.6381557 -2.052121e+00 
[24,] 6.0000000 -7.347881e-16 
[25,] 5.6381557 -2.052121e+00 
[26,] -5.3623111 -4.499513e+00 
[27,] -4.5962667 -3.856726e+00 
[28,] -6.1283555 -5.142301e+00 
[29,] -4.6984631 -1.710101e+00 
[30,] 0.8682409 -4.924039e+00 
[31,] 2.5000000 -4.330127e+00 
[32,] -0.8682409 -4.924039e+00 
[33,] -6.0000000 1.469576e-15 
[34,] -5.3623111 -4.499513e+00 
[35,] -3.8302222 -3.213938e+00 
[36,] -4.5962667 -3.856726e+00 
[37,] -3.5000000 -6.062178e+00 
[38,] 1.0418891 -5.908847e+00 
[39,] 5.3623111 -4.499513e+00 
[40,] 4.5962667 -3.856726e+00 
[41,] 3.8302222 -3.213938e+00 
[42,] 3.0000000 -5.196152e+00 
[43,] 5.3623111 -4.499513e+00 
[44,] 5.3623111 -4.499513e+00 
[45,] 4.5962667 -3.856726e+00 
[46,] 3.0000000 -5.196152e+00 
[47,] 4.5962667 -3.856726e+00 
[48,] 3.8302222 -3.213938e+00 
[49,] 1.0418891 -5.908847e+00 
[50,] 3.8302222 -3.213938e+00 

你可以在這裏找到一個矢端曲線例如:enter image description here(如在左下角的一個)。

有了這些hodograms(每月1個),我想用ggplot2來繪製一個刻面圖,但我想(我希望)我可以管理這個部分。

任何幫助,將不勝感激。

非常感謝您提前!

+1

也許你可以告訴我們你到目前爲止所嘗試過的。 –

+0

其實我的問題是開始,導致我的初始數據集是一個方向(0-360°)/強度(m.s-1)表。我將它轉換爲u和v向量,以便嘗試「RSEIS」包中的「hodogram」命令。該軟件包不適合我的需求,並且不提供定製的可能性。所以我試圖在互聯網上找到一個hodogram代碼,但我無法找到任何地方。 –

回答

0

這裏是我的結果。它需要一些改進,但我已經接近我想要的結果,正如帖子中所述。下面是我使用的代碼:給社區#1再次

enter image description here

感謝這始終是真正的反應,樂於助人:

library("ggplot2") 
library("plyr") 
mydata <- read.table("C:\\myfile.csv", sep="\t", header=TRUE) 
seasons <- mydata$seasons 
years <- mydata$year 
u <- mydata$u 
v <- mydata$v 
intensity <- mydata$intensity 
wind <- cbind(u,v,intensity,seasons,years) 
wind <- data.frame(wind) 
x <- ddply(wind, .(years, seasons), summarize, x=cumsum(u*0.0108)) 
y <- ddply(wind, .(years, seasons), summarize, y=cumsum(v*0.0108)) 
x <- x$x 
y <- y$y 
wind <- cbind(wind,x,y) 
wind <- data.frame(wind) 
wind$seasons[wind$seasons == 1] <- "winter" 
wind$seasons[wind$seasons == 2] <- "spring" 
wind$seasons[wind$seasons == 3] <- "summer" 
wind$seasons[wind$seasons == 4] <- "autumn" 
p <- ggplot(wind, aes(x, y)) + geom_path(aes(colour = intensity))+ scale_colour_gradientn(colours=c("blue","yellow","red")) 
p + facet_grid(seasons ~ years) 

給我下面的結果!

2

我得到的東西,我還在做這個工作...

u <- mydata$u 
v <- mydata$v 
x <- cumsum(mydata$u[56297:56704]*10.8) 
y <- cumsum(mydata$v[56297:56704]*10.8) 
wind <- cbind(x,y) 
wind <- data.frame(wind) 
p <- ggplot(wind) + geom_path(aes(x, y, colour = x)) 

enter image description here

未完待續......不要猶豫評論!)

2

這裏我會怎麼做的一個概念。我會讓你擺弄細節(如刪除標題條)。

library(ggplot2) 
library(gridExtra) 
library(MASS) 

linedata <- data.frame(time = rep(1:200, 3), 
         vals = runif(600), 
         source = rep(letters[1:3], each = 200)) 

normdata <- as.data.frame(mvrnorm(n = 600, mu = c(0, 0), Sigma = matrix(c(0.5, 0, 0, 0.5), ncol = 2))) 
normdata$time <- rep(1:200, times = 3) 
normdata$source = rep(letters[1:3], each = 200) 
names(normdata)[1:2] <- c("x", "y") 

linegraph <- ggplot(linedata, aes(x = time, y = vals)) + 
    theme_bw() + 
    geom_line() + 
    facet_wrap(~ source, ncol = 1) 

normgraph <- ggplot(normdata, aes(x = x, y = y)) + 
    theme_bw() + 
    geom_path() + 
    facet_wrap(~ source, ncol = 3) 

grid.arrange(linegraph, normgraph, ncol = 1) 

enter image description here

+0

非常感謝您的回答!我仍然需要使用'cumsum'來獲得真正的hodogram,但是你的代碼將幫助我得到我的最終圖。我將把我的最終結果放在主題中,thx再次! –