【问题标题】:How can I make a feather plot in R?如何在 R 中制作羽毛图?
【发布时间】:2020-11-19 20:02:52
【问题描述】:

是否可以在 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))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    用一点三角函数在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())
    

    【讨论】:

    • 几个月前没有关于矢量可视化的类似问题,我相信你已经给出了答案......?
    • this one @Tjebo 的二维矢量场吗?
    【解决方案2】:

    这是我所追求的最终产品,感谢@Allan Cameron 的帮助

    library(ggplot2)
    library(tidyverse)
    library(dplyr)
    
    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))
    
    wind.df %>%
      ggplot(aes(x = hour, y = 0, angle = direction, radius = speed)) +
      geom_spoke(size = 1,
                   arrow = grid::arrow(length = unit(0.25, "cm"), type = "open")) +
      geom_hline(yintercept = 0, color = "gray50") +
      geom_point() +
      ylab(expression(paste("Absolute Wind Speed (m  ", s^-1,")"))) +
      ylim(-10,10) +
      scale_x_continuous(limits = c(0,12),
                         breaks = c(seq(from = 0, to = 10, by = 1))) +
      theme_bw() +
      theme(panel.grid = element_blank(),
            text = element_text(size = 12),
            axis.text.x = element_text(size = 12, color = "black"),
            axis.text.y = element_text(size = 12, color = "black"))
    

    【讨论】:

      猜你喜欢
      • 2018-08-22
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多