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:
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")
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!!!
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)
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