2013-12-13 99 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再次! –