如何使用更少,更大的箭头使用geom_quiver可视化风数据

时间:2018-03-18 00:23:08

标签: r ggplot2 geospatial

下面的代码覆盖了五大湖的风速和方向。 1)箭头太多了。我如何平均其中大部分,以便在地图上只放置3-5个大箭头?

2)如果它们的速度影响它们的颜色,为什么有些箭头如此之小? 我从帮助(geom_quiver)

中理解这一点
  

vecsize
  默认情况下(NULL),自动确定向量大小调整。如果可以识别网格,则将它们缩放到网格,否则,不会缩放矢量。通过在此处指定数字输入,可以调整所有箭头的长度。将vecsize设置为零将阻止缩放箭头。

我相信这些向量会缩放到网格中。但网格是否均匀分布?

library("ggmap")
library("ggquiver")
library("tidyverse")
library("dplyr")
library("scales")
library(rWind)

# Wind Data
# Global Forecast System (GFS) of the USA's National Weather Service (NWS)
# wind.dl(yyyy, mm, dd, tt, lon1, lon2, lat1, lat2, type = "read-data")
# Time: tt  Options: 00:00 - 03:00 - 06:00 - 09:00 - 12:00 - 15:00 - 18:00 - 21:00 (UTC)
wind_data <- wind.dl(2018, 3, 16, 18,-94, -70, 41, 49)

wind_data_fit <- wind_data %>% 
  wind.fit()

wind_data <- rename(wind_data, "u" = "ugrd10m (m s-1)", "v" = "vgrd10m (m s-1)")
df <- mutate(wind_data_fit, u = wind_data$u, v = wind_data$v)

median_speed = median(df$speed)


qmplot(lon, lat, data = df, 
       extent = "panel", 
       geom = "blank", 
       zoom = 6, 
       maptype = "watercolor") + 

  scale_colour_gradient2(low = "white", mid = "black",
                         high = "firebrick", midpoint = median_speed, space = "Lab",
                         na.value = "grey50", guide = "colourbar") +

  geom_quiver(aes(u = u, v = v, colour = speed),
              center = TRUE,
              alpha = 0.7,
              size = 0.7, # Line width
              vecsize = 2) # Length of straight segment

1 个答案:

答案 0 :(得分:3)

geom_quiver正在为每一行数据绘制一个箭头。减少数字箭头的一个选项是为每个 n 行的数据绘制一个箭头。在下面的示例中,我们绘制每隔一行(因此 n = 2 ),将箭头数减半。唯一需要做的更改是在第一行绘图代码的data参数中:

qmplot(lon, lat, data = df[seq(1,nrow(df),2), ], 

enter image description here

将其与原始情节进行比较:

enter image description here

您可以通过选择要在地图上绘制的任何数据行来进一步缩小箭头。请参阅下面的示例。在这种情况下,请注意我已将完整数据帧df用作qmplot的数据参数,并将过滤后的数据帧用作geom_quiver的数据参数。这是为了确保地图包含整个数据框的整个纬度范围,即使我们只绘制了几个点的箭头:

# Latitudes to keep
lat.keep = unique(df$lat)[seq(2, length(unique(df$lat)), 5)]

qmplot(lon, lat, data = df,
       extent = "panel", geom="blank", zoom = 6, maptype = "watercolor") + 
  scale_colour_gradient2(low = "yellow", mid = "black",
                         high = "firebrick", midpoint = median_speed, space = "Lab",
                         na.value = "grey50", guide = "colourbar") +
  geom_quiver(data=df %>%
                 filter(lat %in% lat.keep) %>% 
                 group_by(lat) %>%
                 slice(seq(2, n(), 11)), # Keep 1/11th of longitudes
              aes(u = u, v = v, colour = speed),
              center = TRUE, alpha = 1, size = 1, vecsize = 0.9) 

enter image description here