ggplot2 wind time series with arrows/vectors

后端 未结 2 1929
攒了一身酷
攒了一身酷 2020-12-17 22:45

From meteorological data (hourly values of temperature, wind and humidity) I managed to plot time series of wind speed and direction. Now I would like to add wind vectors on

相关标签:
2条回答
  • 2020-12-17 22:48

    Just as a preamble, please make sure you include all code and relevant data in future questions. If you look at your question above, you will see that some objects such as torre are not defined. That means we can't copy and paste into our R setups. Also the data to which you linked could not be used with the code in the question as it was a limited subset. My advice: (a) create fake data that looks like the data you are using (b) keep your code to the absolute minimum (c) test and double-check code and data in a new R session before you post.

    As far as I can tell you want something like the below. Of course you will have to adapt it for your own purposes, but it should give you some ideas on how to tackle your problem. Notice most of the cosmetic properties such as line colours, thicknesses, legends and titles have been omitted from the plot: they are not important for the purposes of this question. EDIT Another approach might be to use the same data frame for the wind data and then use a faceting variable to show the speed in a different but linked plot.

    require(ggplot2)
    require(scales)
    require(gridExtra)
    require(lubridate)
    set.seed(1234)
    
    # create fake data for temperature
    mydf <- data.frame(datetime = ISOdatetime(2013,08,04,0,0,0) +
                       seq(0:50)*10*60,
                       temp = runif(51, 15, 25))
    
    # take a subset of temperature data,
    # basically sampling every 60 minutes
    wind <- mydf[minute(mydf$datetime) == 0, ]
    # then create fake wind velocity data
    wind$velocity <- runif(nrow(wind), -5, 20)
    # define an end point for geom_segment
    wind$x.end <- wind$datetime + minutes(60)
    
    ggplot(data = mydf, aes(x = datetime, y = temp, group = 1)) +
        geom_line() +
        geom_segment(data = wind,
                     size = 3,
                     aes(x = datetime,
                         xend = x.end,
                         y = 10,
                         yend = velocity),
                     arrow = arrow(length = unit(0.5, "cm"))) +
        theme()
    

    This generates the following plot: screenshot

    0 讨论(0)
  • 2020-12-17 22:51

    Compute the direction of the wind using decimal degrees. Assuming that you want 0 degrees to be North (up), use the following:

    ggplot(data = wind, aes(x=datetime, y=temp)) + 
      geom_text(aes(angle=-wind_dir_degrees+90), label="→")
    
    0 讨论(0)
提交回复
热议问题