r - 如何在 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))
解决方案
用一点三角函数在 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())
推荐阅读
- c# - 如何在Windows的C/C#/cmd中检查两个文件名是否指向同一个物理文件?
- amazon-web-services - 如何在 amazon-ec2 上录制语音?
- php - 将php网站部署到heroku时出现自动加载器问题
- python - networkx 中的多层图
- flex-lexer - flex 的条件
- javascript - 如何将 ajax 成功数据中的值显示到 html 表中?
- javascript - 无法在 firebase 中使用数据 - 错误:FIREBASE 致命错误:无法解析 Firebase url。请使用 https://
.firebaseio.com - node.js - 不能在模块外使用 import 语句
- r - R中是否有一种快速的方法来预测来自RANN的最近邻模型的观察的类结果?
- wordpress - Wordpress 自定义字段 - 嵌套 foreach