3
ggplot
, tidyverse
, lubridate
에 대해 쉽게 작업해야한다고 생각하지만 우아한 해결책을 찾지 못하는 것 같습니다.그룹화 된 날짜 변수 (예 : year_month)를 사용하는 ggplot
목표 : 연도 및 월별 집계/요약/그룹화 된 데이터로 막대 그래프를 만듭니다.
#Libraries
library(tidyverse)
library(lubridate)
# Data
date <- sample(seq(as_date('2013-06-01'), as_date('2014-5-31'), by="day"), 10000, replace = TRUE)
value <- rnorm(10000)
df <- tibble(date, value)
# Summarise
df2 <- df %>%
mutate(year = year(date), month = month(date)) %>%
unite(year_month,year,month) %>%
group_by(year_month) %>%
summarise(avg = mean(value),
cnt = n())
# Plot
ggplot(df2) +
geom_bar(aes(x=year_month, y = avg), stat = 'identity')
year_month 변수를 만들면 자연스럽게 날짜 변수 대신 문자 변수가됩니다. 나는 또한 year(date), month(date)
에 의해 그룹화를 시도했지만 다음 두 변수를 ggplot
에서 x 축으로 사용하는 방법을 알아낼 수 없습니다. 아마 이것은 달의 첫날에 날짜를 마루에 의해 해결할 수 있었다 ...?