ggplot2 segment — direction when x-axis is time

帅比萌擦擦* 提交于 2019-12-07 09:42:44

问题


I have a wind speed and direction times series and I am trying to make a plot that uses line segments to depict the windspeed and direction. I need to have a basic point scatter plot of windspeed vs time (which I can create) with a line segment from each point (which is where I am getting hung up.) The length of the line has to be proportional to the windspeed and the angle needs to the wind direction. I am using ggplot2 with the geom_segment, but because x is time I cannot figure the right formula to use for xend.

Here is an example of the dataframe:

  • DateTime WINDSPEED_MPH DIR
  • 8/29/2008 0:00 4.28 231
  • 8/29/2008 1:00 3.11 236
  • 8/29/2008 2:00 1.36 237
  • 8/29/2008 3:00 2.92 153
  • 8/29/2008 4:00 1.94 314
  • 8/29/2008 5:00 3.11 293
  • Here is my code so far:

    library(ggplot2) 
    library(lubridate)
    
    Gustav <- read.csv("C:/Users/ezco3/My Research/LPBF/Pontchartrain-Maurepas Surge Consortium/Projects/Lake Tilting Effect Graphic/Datasets/Gustav_NewCanal_Hydro&Metero.csv")
    
    Gustav$TS <- as.POSIXct(Gustav$DateTime, "%m/%d/%Y %H:%M", tz="America/Chicago")
    Gustav$DelY <- Gustav$WINDSPEED_MPH*sin(Gustav$DIR)/max(Gustav$WINDSPEED_MPH, na.rm=TRUE)
    Gustav$DelX <- Gustav$WINDSPEED_MPH*cos(Gustav$DIR)/max(Gustav$WINDSPEED_MPH, na.rm=TRUE)
    
    plt1 <- ggplot(data = Gustav, aes(x = TS, y =WINDSPEED_MPH))
    plt1 + geom_point(color="blue") + 
      geom_segment(data = Gustav, mapping=aes(x= TS, y = WINDSPEED_MPH, xend = TS + 18000*DelX, 
                   yend=WINDSPEED_MPH + 5*DelY),size=.1,color="red")
    

    And, that creates this:

    The 18000 in front of DelX and the 5 in front of DelY are both arbitrary numbers that I found through trail and error. Basically, initially using just DelX and DelY (without multipliers) the lines were short and vertical, so I worked through different option until I found this pair which showed some direction to the line.

    However, the angles are not correct. At this stage, I do not understand how units work with time series objects in lubridate so I am lost as to how to figure the correct formula for xend.

    Any advice would be much appreciated.

    Thanks!!!


    回答1:


    Using the suggestions of @eipi10 to calculate the appropriate ratio and multiply by the change in x but not explicitly setting the coordinate limits, here's a solution that correctly displays wind directions of 45 degrees and the length of the segment corresponds to the windspeed.

    date <- seq(ymd('2012-04-07'),ymd('2013-03-22'), by = 'day')
    windspeed <- rnorm(n = length(date), mean = 5, sd = 1)
    dir <- rep((c(0,30,45,60,90)*(pi/180)), times = length(date)/5)
    
    data <- data.frame(date, windspeed, dir)
    
    rat <-as.numeric(interval(min(date), max(date))) / (max(windspeed) - min(windspeed))
    
    data <- data %>%
        mutate(DelY = windspeed * sin(dir)/(2*max(windspeed)),
               DelX  = rat * windspeed * cos(dir)/(2*max(windspeed)))
    
    
    
    plt1 <- ggplot(data = data, aes(x = date, y =windspeed))
    plt1 + geom_point(color="blue")+
      geom_segment(aes(x= date, y = windspeed, xend = date + DelX,
                       yend=windspeed + DelY),size=.1,color="red") +
      coord_fixed(ratio = rat)
    



    回答2:


    If you are open to an alternative to geom_segment(), then you can use geom_text() with an ANSI (unicode) arrow symbol to get the desired output:

    ggplot(data = data, aes(x=date, y=windspeed)) + 
      geom_point() + 
      geom_text(aes(angle=-dir+90), label="→")
    


    来源:https://stackoverflow.com/questions/33809675/ggplot2-segment-direction-when-x-axis-is-time

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!