当我使用条形图绘制每小时的数据时,条形图被x轴的刻度线分割。数据对应于6-7点,7-8点,以此类推。我希望每个条形在x‘时钟整点开始,而不是x- 0:30分钟。
代码示例:
install.packages("nycflights13")
library(nycflights13)
data <- flights %>% select(time_hour, hour) %>%
mutate(flightdate = floor_date(time_hour, "day")) %>%
filter(flightdate == as_date("2013-01-01")) %>% group_by(flightdate,hour) %>% tally() %>%
mutate(flightdatetime = as_datetime(paste(flightdate + hours(hour)))) %>%
select(flightdatetime,n)
ggplot() +
geom_col(aes(x=flightdatetime, y = n),
data = data, color = "black", fill = "black") +
ggplot2::scale_x_datetime(labels = date_format("%H:%M", tz = "Europe/Berlin"), date_breaks = "1 hour") +
theme_minimal() + labs(title ="Flights per hour")
发布于 2018-08-23 22:06:11
这样如何:
ggplot() +
geom_bar(aes(x=flightdatetime, y = n),stat="identity",
data = data, color = "black", fill = "black") +
ggplot2::scale_x_datetime(labels = date_format("%H:%M", tz = "Europe/Berlin"), date_breaks = "1 hour", expand=c(0, .9)) +
theme_minimal() + labs(title ="Flights per hour") +
theme(axis.text.x = element_text(hjust=1.5))
我使用theme(axis.text.x = element_text(hjust=1.5))
向左移动标签,并在scale_x_datetime
中使用expand=c(0, .9)
来剪切第一个和最后一个轴标签。你可以试一下这两个,看看它是否合适。
https://stackoverflow.com/questions/51987264
复制相似问题