我希望在几个月内创建一个小时的循环图。我希望它看起来像下面的情节。我的目标是用水平线指示每个月的平均温度,然后在每个月内用图表显示该月典型日的温度波动。我试图使用monthplot()
,但似乎没有工作:
library(nycflights13)
tempdata <- weather %>% group_by(hour)
monthplot(tempdata, labels = NULL, ylab = "temp")
它一直说argument is not numeric or logical: returning NA
,但我不确定代码出错的地方。
答案 0 :(得分: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())
答案 1 :(得分:2)