【发布时间】:2017-09-04 13:05:23
【问题描述】:
我想隐藏 ggplot2 中没有数据的列。这是使用 nycflights13 库的可重现示例:
library(nycflights13)
library(dplyr)
library(ggplot2)
small_data <- flights %>%
mutate(date = lubridate::make_date(year, month, day)) %>%
filter(year == 2013,
month ==3)
ggplot(small_data) +
geom_bar(aes(date)) +
scale_x_date(date_labels = "%d - %b", date_breaks = "1 day") +
theme(axis.text.x = element_text(angle = 65, hjust = 1))
正如你所看到的,虽然我只选择了 3 月的数据,但我有 2 月 28 日和 4 月 1 日的列标题。
我尝试了以下方法:
hide missing dates from x-axis ggplot2(该解决方案不再有效)
设置(date_)breaks = levels(factor(small_data$date))
请帮我隐藏不必要的栏。
【问题讨论】:
-
选项 expand=c(0,0) 调整 x 比例,使其不显示不必要的日期。添加 "scale_x_date(date_labels = "%d - %b", date_breaks = "1 day", expand=c(0,0))"