Search code examples
rvectorplotdirection

Hodogram using ggplot2


I would like to create a facetted plot using ggplot2, representing monthly wind hodograms.

I recently read the following post : How to map wind direction and speed (velocity plot) with R. I think this is probably a good start for me. The following dataset represents windvalues as u and v components, with a timestep of 3 hours. I would like to represent it as an hodogram, meaning that each vector is following the previous one.

                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

You can find a hodogram example here : enter image description here (like the one on the bottom left corner).

With these hodograms (1 per month), I would like to plot a facetted graph with ggplot2 but I think (I hope) that I can manage this part.

Any help would be appreciated.

Thank you very much by advance !


Solution

  • Here is my result. It needs some improvements, but I am close from the result I wanted as described in the post. Here is the code I used :

    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)
    

    Giving me the following result :

    enter image description here

    Thanks again to the Stackoverflow community which is always really reactive and helpfull !