plot_2 <- flights %>%
filter(origin == "JFK",
month %in% c(7, 8, 9),
!is.na(dep_time)) %>%
left_join(airports %>%
select(faa, name),
by=c("dest"="faa"))
options(repr.plot.width = 9, repr.plot.height = 6)
plot_2 %>%
ggplot(aes(y = reorder(name, name,
function(y) + length(y)))) +
geom_bar(fill = "#4e0090") +
theme_minimal() +
labs(title = "Top Destinations people traveled to the most (summer 2013)",
x = "Count",
y = "Name",
caption = "Source: Package 'nycflights13'") +
theme(plot.title = element_text(hjust = -0.15)) +
expand_limits(x = c(0, 2900))
我确实尝试了top_n()但是没有用,或者我不知道如何处理这些数据结构。 谢谢你的回答!
我可能会在将数据传递给 ggplot2 之前进行操作。
假设你的数据来自
nycflights13
包,下面应该是完全可重现的
library(tidyverse)
nycflights13::flights %>%
filter(origin == "JFK",
month %in% c(7, 8, 9),
!is.na(dep_time)) %>%
left_join(nycflights13::airports %>%
select(faa, name), by=c("dest"="faa")) %>%
filter(!is.na(name)) %>%
count(name) %>%
mutate(name = reorder(name, n)) %>%
top_n(10) %>%
ggplot(aes(y = name, x = n)) +
geom_col(fill = "#4e0090") +
theme_minimal() +
labs(title = "Top Destinations people traveled to the most (summer 2013)",
x = "Count",
y = "Name",
caption = "Source: Package 'nycflights13'") +
theme(plot.title = element_text(hjust = -0.15)) +
expand_limits(x = c(0, 2900))
#> Selecting by n
创建于 2023-02-20 与 reprex v2.0.2