2017-11-11 235 views
2

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

enter image description here

library(nycflights13) 

tempdata <- weather %>% group_by(hour) 

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

口口声声说argument is not numeric or logical: returning NA但我不知道在哪里的代码是怎么了。

回答

4

希望这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()) 

enter image description here

+0

@ njw3498可以请你接受的解决方案,如果它为你工作 – PoGibas

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")) 

enter image description here