2017-11-11 244 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