【问题标题】:Create cycle plot创建循环图
【发布时间】:2017-11-11 14:37:27
【问题描述】:

我希望在几个月内创建一个小时的循环图。我希望它看起来像下面的情节。我的目标是用水平线表示每个月的平均温度,然后在每个月内让图表显示该月典型日子的温度波动。我试图使用monthplot(),但它似乎不起作用:

library(nycflights13)

tempdata <- weather %>% group_by(hour) 

monthplot(tempdata, labels = NULL, ylab = "temp")

它一直说argument is not numeric or logical: returning NA,但我不确定代码哪里出错了。

【问题讨论】:

    标签: r ggplot2 tidyverse


    【解决方案1】:

    希望这个ggplot2 解决方案能够奏效:

    library(nycflights13)
    library(ggplot2)
    library(dplyr)
    
    # Prepare data
    tempdata <- weather %>% 
        group_by(month, day) %>%
        summarise(temp = mean(temp, na.rm = TRUE))
    meanMonth <- tempdata %>%
        group_by(month) %>%
        summarise(temp = mean(temp, na.rm = TRUE))
    
    # Plot using ggplot2
    ggplot(tempdata, aes(day, temp)) +
        geom_hline(data = meanMonth, aes(yintercept = temp)) +
        geom_line() +
        facet_grid(~ month, switch = "x") +
        labs(x = "Month",
             y = "Temperature") +
        theme_classic() +
        theme(axis.ticks.x = element_blank(),
              axis.text.x = element_blank(),
              axis.line.x = element_blank())
    

    【讨论】:

    • @njw3498 如果对您有用,请您接受解决方案
    【解决方案2】:

    temp 缺少导致错误的值。您还需要设置timesphase 参数。

    library(nycflights13)
    
    # Find mean value : this automatically removes the observation with missing data that was causing an error
    tempdata <- aggregate(temp ~ month + day, data=weather, mean) 
    
    with(tempdata, monthplot(temp, times=day , phase=month, ylab = "temp"))
    

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 2013-09-24
      • 2015-08-18
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多