我想在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))
Run Code Online (Sandbox Code Playgroud)
正如yopu所见,虽然我只选择了3月的数据,但我有2月28日和4月1日的专栏文章.
我尝试过以下方法:
从x轴ggplot2隐藏缺少的日期(解决方案不再有效)
设置 (date_)breaks = levels(factor(small_data$date))
请帮我隐藏不必要的酒吧.
或者我们可以设置scale_x_date(..., expand = c(0,0)):
ggplot(small_data) +
geom_bar(aes(date)) +
scale_x_date(date_labels = "%d - %b", date_breaks = "1 day",
expand = c(0,0)) +
theme(axis.text.x = element_text(angle = 65, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
659 次 |
| 最近记录: |