Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

r - Lubridatate month() for multiple years

I'd like to make a plot for 2019 and 2020, but I'm running into a problem with the month() function from lubridate. If I run this:

df  %>%
  mutate (Month = month(Date, label=T)) %>%
  group_by(Month, Var1) %>%
  summarize (sum = sum(numeric_variable)) %>%
ggplot(aes(Month, sum)) +
  geom_col() +
 facet_wrap(. ~ Var1, scales ="free_y")

The data for January 2019 and 2020 and other months are combined in the plot, which makes sense since they're both labelled as 'Jan' in the Month variable for both 2019 and 2020.

How can I best separate the months for 2019 and 2020 while still keeping the label ('janb, 'feb') and the order of my Month variable? Do I have to reorder them as a factor manually or is there a better way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Lubridate is nice for some things, but I much prefer zoo::as.yearmon for months and years. There is even a nice scale_x_yearmon function for ggplot:

library(zoo)
df %>%
  mutate (Month = zoo::as.yearmon(Date)) %>%
  group_by(Month, Var1) %>%
  summarize (sum = sum(numeric_variable)) %>%
ggplot(aes(Month, sum)) +
  geom_col() +
  facet_wrap(. ~ Var1, scales ="free_y") + 
  zoo::scale_x_yearmon(format = "%b")

enter image description here

Sample data:

set.seed(123)
df <- data.frame(Date = rep(seq(as.Date("2019-01-01"),as.Date("2020-12-31"), by = "day"),2),
                                Var1 = rep(LETTERS[1:2],each = 731),
                                numeric_variable = round(runif(2*731,1,100)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...