I added a month_year column to my DF using floor_date. However, now when I plot my geom_bar, I get an extra month at each end.
How do I remove the extra months?
I have tried seq(as.Date)
, but I get the error:
Error in parse_period_unit(unit) : Cannot't parse heterogenuous or fractional units larger than one minute.
I appreciate your help.
The Data:
structure(list(month_year = structure(c(18506, 18506, 18536,
18536, 18567, 18567, 18597, 18597, 18628, 18628, 18659, 18659,
18687, 18687, 18718, 18718, 18748, 18748, 18779, 18779, 18809,
18809, 18840, 18840, 18871, 18871), class = "Date"), member_casual = c("casual",
"member", "casual", "member", "casual", "member", "casual", "member",
"casual", "member", "casual", "member", "casual", "member", "casual",
"member", "casual", "member", "casual", "member", "casual", "member",
"casual", "member", "casual", "member"), n = c(212109L, 284005L,
121479L, 215338L, 72497L, 149370L, 24403L, 89168L, 14590L, 68726L,
8517L, 34296L, 74904L, 129820L, 118822L, 177325L, 213536L, 233461L,
299050L, 303515L, 364569L, 321727L, 337787L, 331900L, 289944L,
327225L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -26L), groups = structure(list(month_year = structure(c(18506,
18506, 18536, 18536, 18567, 18567, 18597, 18597, 18628, 18628,
18659, 18659, 18687, 18687, 18718, 18718, 18748, 18748, 18779,
18779, 18809, 18809, 18840, 18840, 18871, 18871), class = "Date"),
member_casual = c("casual", "member", "casual", "member",
"casual", "member", "casual", "member", "casual", "member",
"casual", "member", "casual", "member", "casual", "member",
"casual", "member", "casual", "member", "casual", "member",
"casual", "member", "casual", "member"), .rows = structure(list(
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -26L), .drop = TRUE))
The code:
monthly_riders %>%
ggplot(aes(x= month_year, y = n, fill=member_casual))
geom_bar(position = "dodge", stat = "identity")
theme(axis.text.x = element_text(angle = 90), legend.title = element_blank())
labs(title = "Total Monthly Riders")
xlab("")
scale_y_continuous(labels = comma,name = "Riders", expand = c(0,0))
scale_x_date(date_labels="%b %y", date_breaks = "1 month")
CodePudding user response:
Add expand=expansion(0,0)
:
monthly_riders %>%
ggplot(aes(x= month_year, y = n, fill=member_casual))
geom_bar(position = "dodge", stat = "identity")
theme(axis.text.x = element_text(angle = 90), legend.title = element_blank())
labs(title = "Total Monthly Riders")
xlab("")
scale_y_continuous(labels = comma,name = "Riders", expand = c(0,0))
scale_x_date(date_labels="%b %y", date_breaks = "1 month", expand = expansion(0,0))
CodePudding user response:
You can use the limits
function:
`
monthly_riders %>% ggplot(aes(x= month_year, y = n, fill=member_casual))
geom_bar(position = "dodge", stat = "identity")
theme(axis.text.x = element_text(angle = 90), legend.title = element_blank())
labs(title = "Total Monthly Riders")
xlab("")
scale_y_continuous(name = "Riders", expand = c(0,0))
scale_x_date(date_labels="%b %y", date_breaks = "1 month", limits=as.Date(c("2020-09-01","2021-09-01")))
`
Note that I removed the "labels = comma," as it was giving me an error.