删除 R geom_bar 中多余的月份
Remove extra month in R geom_bar
我使用 lubridate::floor_date
在我的 data.frame 中添加了一个 'month_year' 列。然而,现在当我绘制我的 geom_bar 时,我在每一端都多了一个月。
如何删除多余的月份?
我试过 seq(as.Date)
,但我收到错误:
Error in parse_period_unit(unit) : Cannot't parse heterogenuous or
fractional units larger than one minute.
感谢您的帮助。
数据:
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))
代码:
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")
您可以使用limits
函数:
`
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")))
`
请注意,我删除了“标签 = 逗号”,因为它给我一个错误。
添加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))
我使用 lubridate::floor_date
在我的 data.frame 中添加了一个 'month_year' 列。然而,现在当我绘制我的 geom_bar 时,我在每一端都多了一个月。
如何删除多余的月份?
我试过 seq(as.Date)
,但我收到错误:
Error in parse_period_unit(unit) : Cannot't parse heterogenuous or fractional units larger than one minute.
感谢您的帮助。
数据:
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))
代码:
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")
您可以使用limits
函数:
`
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")))
`
请注意,我删除了“标签 = 逗号”,因为它给我一个错误。
添加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))