带有箭头/矢量的ggplot2风时间系列 [英] ggplot2 wind time series with arrows/vectors

查看:204
本文介绍了带有箭头/矢量的ggplot2风时间系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据气象数据(温度,风和湿度的小时数值),我设法绘制了风速和风向的时间序列。现在我想在时间序列图上添加风向量。在这里,你可以看到我想要的输出(箭头绘制在实际情节中)。


我使用ggplot2,一直在寻找通过stackoverflow,ggplot2软件包文档(将继续),但没有找到解决方案。任何想法或指示在哪里看将是一个起点。



预先感谢

编辑问题
正如@slowlearner的评论中所建议的那样,我在此添加代码和数据以创建可重复的示例。它看起来像geom_segment可以做到这一点。我设法计算geom_segment中的明细,但无法弄清楚如何找到xend,因为x轴是时间轴。我有风速和风向数据,因此我可以计算geom_segment的x,y风分量,但需要将x转换为时间格式。



以下是用于绘图(温度)和 data

  for(i in 1:2){

rams = subset(data,数据$ stat_id%in%i)
tore =子集(torre,torre $ stat_id%in%i)

#Gàficaen ggplot

#Gráficadeevolución temporal de las estaciones de la zona

gtitol = places $ nom [places $ stat_id == i]

myplot = ggplot(data = rams,aes(x = datetime, y = tempc,color =RAMS))+
geom_line()+ ylab(Temperatura(ºC))+ xlab()+
ggtitle(gtitol)+ theme(legend.title = data_blank())+ theme(legend.position =bottom)+
scale_y_continuous(limits = c(0,40))+
geom_line(data = tore,aes(x = datetime,y )=
$#
$($)
$ = $($) b
$ b ggsave(myplot,filename = paste(RAMS - ,i,。png,sep =),width = 7.78,height = 5.79)

}

继geom_segment文档后,我试过了

<$ p $ (x = datetime,y = 5))
p + geom_segment(aes(xend = datetime,yend = 5 + v),arrow = arrow(length =单位(0.1,cm)))

获得这种数字。你可以看到x坐标是日期时间,我应该如何将风组件转换为xend?现在xend等于日期时间,但我想像日期时间+ xwindcomp




感谢您的帮助

解决方案

正如序言一样,请确保在未来的问题中包含所有代码和相关数据。如果你看看上面的问题,你会发现一些对象如 torre 没有定义。这意味着我们无法复制并粘贴到我们的R设置中。此外,您链接的数据不能用于问题中的代码,因为它是一个有限的子集。我的建议是:(a)创建类似于你正在使用的数据的伪造数据(b)保持代码的绝对最小值(c)测试并在新的R会话之前仔细检查代码和数据你发布。



据我可以告诉你需要类似下面的内容。当然,你将不得不为了你自己的目的去适应它,但它应该给你一些关于如何解决你的问题的想法。注意大部分化妆品属性,例如线条颜色,厚度,图例和标题已从绘图中省略:它们对于此问题的目的并不重要。编辑另一种方法可能是对风数据使用相同的数据框,然后使用分面变量在不同但链接的图中显示速度。

  require(ggplot2)
require(scale)
require(gridExtra)
require(lubridate)
set.seed(1234)

#为温度
创建假数据mydf< - data.frame(datetime = ISOdatetime(2013,08,04,0,0,0)+
seq(0:50)* 10 * 60,
temp = runif(51,15,25))

#取一部分温度数据,
#基本上每60分钟取样一次
风< - mydf [minute(mydf $ datetime)== 0,]
#然后创建假风速数据
wind $ velocity< - runif(nrow(wind),-5,20)
#定义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 =日期时间,
xend = x.end,
y = 10,
yend =速度),
arrow =箭头(长度=单位(0.5,cm)) ))+
theme()

这会产生如下图:
< img src =https://i.stack.imgur.com/5M4qW.pngalt =screenshot>


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 the time series plot. Here you can see the output I would like (arrows drawn over the actual plot).

I am using ggplot2, been looking through stackoverflow, ggplot2 package docs (will continue) but no solution found. Any idea or indication where to look would be a starting point.

Thanks in advance

EDIT QUESTION As suggested in a comment by @slowlearner I add here code and data to make a reproducible example. It looks like geom_segment could do the trick. I managed to calculate yend in geom_segment but can't figure out how to find xend as x axis is a time axis. I've got wind speed and direction data so I can calculate x,y wind components for geom_segment but x needs to be converted to time format.

Here is the code used for the plot (temperature) and data

for (i in 1:2 ) {

rams=subset(data,data$stat_id %in% i)
tore=subset(torre,torre$stat_id %in% i)

# Gràfica en ggplot

# Gráfica de evolución temporal de las estaciones de la zona

gtitol=places$nom[places$stat_id == i]

myplot=ggplot(data=rams,aes(x=datetime, y=tempc, colour="RAMS")) +
  geom_line()  +  ylab("Temperatura (ºC)") + xlab(" ") + 
  ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
  scale_y_continuous(limits = c(0,40)) +
  geom_line(data=tore,aes(x=datetime,y=temp,colour = "Torre")) 
#scale_y_continuous(breaks = round(seq(min(data$tempc), max(data$tempc), by = 2),0))


ggsave(myplot,filename=paste("RAMS-",i,".png",sep=""),width=7.78,height=5.79)

}

Following geom_segment docs I tried

p=ggplot(tore, aes(x =datetime, y = 5))
p + geom_segment(aes(xend = datetime, yend = 5 + v), arrow = arrow(length = unit(0.1,"cm")))

Getting this kind of figure. You can see x coordinate is datetime, how should I convert wind component to get xend? By now xend equals datetime but I would like something like "datetime + xwindcomp"

Thanks for your help

解决方案

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:

这篇关于带有箭头/矢量的ggplot2风时间系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆