首页 > 解决方案 > 如何在 R 中制作羽毛图?

问题描述

是否可以在 R 中制作羽毛图?做一些谷歌搜索只发现了一个 R 包 ( feather.plot) 能够制作羽毛图,但它是一个旧包,不适用于 R 版本 3.6.1。下面是一个风速和风向时间序列的例子,我想用它来制作羽毛图。x 轴是hour,每根羽毛的长度应该是speed,每根羽毛的角度应该是direction

set.seed(123)

wind.df <- data.frame(hour = 1:10,
                      speed = runif(n=10, min = 1, max = 10),
                      direction <- runif(n=10, min = 0, max = 360))

标签: rggplot2

解决方案


用一点三角函数在 ggplot 中做出这样的事情并不需要太多的努力。这是使用您的示例数据的完整代表:

set.seed(123)

wind.df <- data.frame(hour = 1:10,
                      speed = runif(n = 10, min = 1, max = 10),
                      direction = runif(n = 10, min = 0, max = 360))

library(dplyr)
library(ggplot2)

wind.df %>% 
  mutate(yend = speed * cos(direction * 2 * pi / 360) * 0.1,
         xend = speed * sin(direction * 2 * pi / 360) * 0.1 + hour) %>%
  ggplot(aes(x = hour, y = 0)) +
  geom_segment(aes(xend = xend, yend = yend), size = 1,
               arrow = grid::arrow(length = unit(0.15, "inches"), type = "closed")) +
  geom_hline(yintercept = 0, color = "gray50") +
  scale_x_continuous(breaks = 1:10) +
  geom_point() +
  labs(y = "") +
  coord_equal() +
  theme_bw() +
  theme(axis.text.y = element_blank())

在此处输入图像描述


推荐阅读