本文介绍了ggplot2段 - 当x轴是时间时的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个风速和风向时间序列,我正在试图用线段来描绘风速和方向。我需要从每个点(这是我挂起的位置)的线段得到风速对时间(我可以创建)的基点散点图。线的长度必须与风速成比例,该角度需要风向。我正在使用ggplot2和geom_segment,但是因为x是时间,我无法找到正确的公式来用于xend。 下面是一个数据框的例子: 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 以下是我的代码: $ b $ 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 Gustav $ DelY 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) 然后,创建这个: DelX前面的18000和DelY前面的5个都是通过跟踪和错误找到的任意数字。基本上,最初只使用DelX和DelY(没有乘法器)线是短而垂直的,所以我通过不同的选择,直到我发现这对显示一些方向的线。 然而,角度不正确。在这个阶段,我不明白单元如何与lubridate中的时间序列对象一起工作,所以我迷失于如何计算xend的正确公式。 任何建议都将非常感谢。 谢谢! 解决方案使用@ eipi10的建议计算合适的比率并乘以x中的更改,但不明确设置坐标限制,这里有一个正确显示45度风向的解决方案,并且该段的长度对应于风速。 date< - seq(ymd('2012-04-07'),ymd('2013-03-22'),by ='day') windspeed dir data rat data >% mutate(DelY = windspeed * sin(dir)/(2 * max(windspeed)), DelX = rat * windspeed * cos(dir)/(2 * max(windspeed)) $ b plt1< - ggplot(data = data, aes(x =日期,y =风速)) plt1 + geom_po int(color =blue)+ geom_segment(aes(x = date,y = windspeed,xend = date + DelX, yend = windspeed + DelY),size = .1,color =红色)+ coord_fixed(ratio = rat) 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!!! 解决方案 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) 这篇关于ggplot2段 - 当x轴是时间时的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 02:35