所以我有一个名为date的变量,我通过使用这两行代码来提取月份和年份:
month_of_date <- month(as.POSIXlt(housing_data$date, format="%Y-%m-%d"))
year_of_date <- year(as.POSIXlt(housing_data$date, format="%Y-%m-%d"))
然后我使用这行代码组合它:
month_year_of_date <- paste(month_of_date, year_of_date, sep = "/")
如何将数据汇总到月/年级别,并在X轴上绘制月/年图表以使其按顺序排列?
这是我到目前为止的图表,但它不是有序的。
图表代码:
ggplot(housing_data, aes(x = factor(month_year_of_date), y = housing_data$price)) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) +
geom_line()
答案 0 :(得分:2)
假设最后在Note中给出housing_data
这两行将它转换为带有yearmon索引的zoo,然后使用autoplot.zoo绘制它。
library(ggplot2)
library(zoo)
z <- read.zoo(housing_data, index = "date", FUN = as.yearmon)
autoplot(z, geom = "blank", width = .01) + geom_bar(stat = "identity") + scale_x_yearmon()
housing_data <-
data.frame(price = 1:12, date = c("2000-01-01",
"2000-02-01", "2000-03-01", "2000-04-01", "2000-05-01", "2000-06-01",
"2000-07-01", "2000-08-01", "2000-09-01", "2000-10-01", "2000-11-01",
"2000-12-01"))
答案 1 :(得分:1)
次要注意:这是我真的不喜欢以月为先的日期陈述的原因之一。如果你可以忍受年月/月,年月或类似的事情,这不是必要的......但我离题了。
解决它的方法与factor
无关,尽管它会受益于此修复。由于您已经在使用set.seed(2)
random_dates <- as.Date(Sys.Date() + sample(1000, size=20))
month_of_date <- lubridate::month(random_dates)
year_of_date <- lubridate::year(random_dates)
month_year_of_date <- paste(month_of_date, year_of_date, sep = "/")
month_year_of_date
# [1] "11/2018" "4/2020" "11/2019" "10/2018" "11/2020" "11/2020" "9/2018"
# [8] "8/2020" "8/2019" "10/2019" "10/2019" "12/2018" "5/2020" "10/2018"
# [15] "6/2019" "8/2020" "12/2020" "12/2018" "7/2019" "7/2018"
,因此更加容易。定义因子时,隐式定义顺序。
两种方法:
使用提供的数据,没有额外的级别。
order
这些都是乱序的,所以我们使用ordered_month_year_of_date <- unique(month_year_of_date[ order(year_of_date, month_of_date) ])
ordered_month_year_of_date
# [1] "7/2018" "9/2018" "10/2018" "11/2018" "12/2018" "6/2019" "7/2019"
# [8] "8/2019" "10/2019" "11/2019" "4/2020" "5/2020" "8/2020" "11/2020"
# [15] "12/2020"
,按年和月变量:
month_year_of_date <- factor(month_year_of_date, levels = ordered_month_year_of_date)
现在定义因子
set.seed(2)
random_dates <- as.Date(Sys.Date() + sample(1000, size=20))
month_of_date <- lubridate::month(random_dates)
year_of_date <- lubridate::year(random_dates)
ordered_date_range <- format(do.call(seq, c(as.list(range(random_dates)), by="month")),
format = "%m/%Y")
head(ordered_date_range)
# [1] "07/2018" "08/2018" "09/2018" "10/2018" "11/2018" "12/2018"
定义一整套可能的月份;这会更大,但是如果你希望在某个时候扩展数据集,那么它们之间的所有点都已经被覆盖了。
factor
前导零将会出现ordered_date_range <- gsub("^0", "", ordered_date_range)
head(ordered_date_range)
# [1] "7/2018" "8/2018" "9/2018" "10/2018" "11/2018" "12/2018"
month_year_of_date <- factor(paste(month_of_date, year_of_date, sep = "/"),
levels = ordered_date_range)
,因此我们将其删除:
month_year_of_date
# [1] 11/2018 4/2020 11/2019 10/2018 11/2020 11/2020 9/2018 8/2020 8/2019
# [10] 10/2019 10/2019 12/2018 5/2020 10/2018 6/2019 8/2020 12/2020 12/2018
# [19] 7/2019 7/2018
# 30 Levels: 7/2018 8/2018 9/2018 10/2018 11/2018 12/2018 1/2019 ... 12/2020
sort(month_year_of_date)
# [1] 7/2018 9/2018 10/2018 10/2018 11/2018 12/2018 12/2018 6/2019 7/2019
# [10] 8/2019 10/2019 10/2019 11/2019 4/2020 5/2020 8/2020 8/2020 11/2020
# [19] 11/2020 12/2020
# 30 Levels: 7/2018 8/2018 9/2018 10/2018 11/2018 12/2018 1/2019 ... 12/2020
从这里开始,排序&#34;只是工作&#34;:
ggplot(housing_data, aes(x = month_year_of_date, y = housing_data$price)) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) +
geom_line()
这将使您的(完全未经测试的)绘图代码类似于:
factor
(即,没有counNodes()
,因为它已经完成了。)
答案 2 :(得分:0)
月份将从月份值中删除前导零。例如,月份&#34; 03&#34;输出为&#34; 3&#34;。要将输出作为&#34; 03&#34;,请尝试按如下方式获取月份和年份。
year_of_date <- format(as.POSIXlt(housing_data$date, format="%Y-%m-%d"),"%Y")
month_of_date <- format(as.POSIXlt(housing_data$date, format="%Y-%m-%d"),"%m")
month_year_of_date <- paste(year_of_date, month_of_date, sep = "/")