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

library(nycflights13)
tempdata <- weather %>% group_by(hour)
monthplot(tempdata, labels = NULL, ylab = "temp")它一直显示argument is not numeric or logical: returning NA,但我不确定代码出了什么问题。
发布于 2017-11-11 22:53:29
希望这个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())

发布于 2017-11-12 00:04:40
temp缺少一个值,这会导致错误。您还需要设置times和phase参数。
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"))

https://stackoverflow.com/questions/47239057
复制相似问题