【发布时间】:2020-06-14 19:43:06
【问题描述】:
我正在使用 R 进行练习,并且在尝试创建每月航空公司乘客图表时遇到了障碍。
我想显示从 1949 年到 1960 年的每一年的单独月线图,据此记录数据。为此,我使用 ggplot 创建了一个包含每月值的折线图。这很好用,但是当我尝试使用 facet_wrap() 将其按年分开并格式化当前的month 字段时:facet_wrap(format(air$month[seq(1, length(air$month), 12)], "%Y"));它返回这个:
我还尝试通过输入我自己多年来的序列来格式化构面:rep(c(1949:1960), each = 12)。这会返回一个更好但仍然错误的不同结果:
这是我的代码:
air = data.frame(
month = seq(as.Date("1949-01-01"), as.Date("1960-12-01"), by="months"),
air = as.vector(AirPassengers)
)
ggplot(air, aes(x = month, y = air)) +
geom_point() +
labs(x = "Month", y = "Passengers (in thousands)", title = "Total passengers per month, 1949 - 1960") +
geom_smooth(method = lm, se = F) +
geom_line() +
scale_x_date(labels = date_format("%b"), breaks = "12 month") +
facet_wrap(format(air$month[seq(1, length(air$month), 12)], "%Y"))
#OR
facet_wrap(rep(c(1949:1960), each = 12))
那么我如何每年制作一张单独的图表?
谢谢!
【问题讨论】:
标签: r ggplot2 facet-wrap