pac*_*met 8 r time-series ggplot2
根据气象数据(温度,风和湿度的小时值),我设法绘制了风速和风向的时间序列.现在我想在时间序列图上添加风向量.在这里你可以看到我想要的输出(在实际情节上绘制的箭头).
我正在使用ggplot2,一直在查看stackoverflow,ggplot2包文档(将继续)但找不到解决方案.任何想法或指示在哪里看都是一个起点.
提前致谢
编辑问题 正如@slowlearner在评论中所建议的那样,我在这里添加代码和数据以制作可重现的示例.貌似geom_segment可以做到这一点.我设法在geom_segment中计算yend但是无法弄清楚如何找到xend,因为x轴是时间轴.我有风速和风向数据,所以我可以计算geom_segment的x,y风成分,但x需要转换为时间格式.
这是用于绘图(温度)和数据的代码
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)
}
Run Code Online (Sandbox Code Playgroud)
我尝试了geom_segment文档
p=ggplot(tore, aes(x =datetime, y = 5))
p + geom_segment(aes(xend = datetime, yend = 5 + v), arrow = arrow(length = unit(0.1,"cm")))
Run Code Online (Sandbox Code Playgroud)
得到这种形象.你可以看到x坐标是datetime,我应该如何转换wind组件来获取xend?到现在为止xend等于datetime但我希望像"datetime + xwindcomp"这样的东西
谢谢你的帮助
作为序言,请确保在以后的问题中包含所有代码和相关数据。如果您查看上面的问题,您会发现某些对象(例如)torre
未定义。这意味着我们无法复制并粘贴到 R 设置中。此外,您链接到的数据无法与问题中的代码一起使用,因为它是有限的子集。我的建议:(a) 创建与您正在使用的数据相似的虚假数据 (b) 将代码保持在绝对最低限度 (c) 在发布之前在新的 R 会话中测试并仔细检查代码和数据。
据我所知你想要类似下面的东西。当然,您必须根据自己的目的对其进行调整,但它应该为您提供一些如何解决问题的想法。请注意,大多数装饰属性(例如线条颜色、粗细、图例和标题)已从图中省略:对于本问题而言,它们并不重要。编辑另一种方法可能是对风数据使用相同的数据框,然后使用分面变量在不同但链接的图中显示速度。
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()
Run Code Online (Sandbox Code Playgroud)
这会生成以下图: