无法让 ggplot2 制作条形图
cant get ggplot2 to make a barplot
# vector of storm types **in required order**
all_statuses <- c("hurricane","tropical storm", "tropical depression")
# convert `status` variable to a factor
storms <- storms %>%
mutate(status=factor(month.abb[month], levels = month.abb))
storms <- storms %>%
mutate(status = factor(status, levels = all_statuses))
# use updated storms data to make bar plot
ggplot(storms,
# N.B. species **already converted to factor**
aes(x = month_fac, fill = all_statuses)) +
# use geom_bar to add bar plot layer with stacked bars
geom_bar()
美学必须是长度 1 或与数据相同 (11859):填充 - 我不断收到的错误
任何帮助将不胜感激
我会管一切:
library(ggplot2)
library(dplyr)
library(forcats)
storms %>%
mutate(month_fac = factor(month.abb[month], levels = month.abb), ### as you did
order = case_when(status == "hurricane" ~ 1,
status == "tropical storm" ~ 2,
status == "tropical depression" ~ 3), ### create order
all_statuses = fct_reorder(status, order)) %>% ### create factor
ggplot(aes(x = month_fac, fill = all_statuses)) +
geom_bar()
输出为:
请注意,没有二月和三月的风暴数据。您需要为此添加空行。
# vector of storm types **in required order**
all_statuses <- c("hurricane","tropical storm", "tropical depression")
# convert `status` variable to a factor
storms <- storms %>%
mutate(status=factor(month.abb[month], levels = month.abb))
storms <- storms %>%
mutate(status = factor(status, levels = all_statuses))
# use updated storms data to make bar plot
ggplot(storms,
# N.B. species **already converted to factor**
aes(x = month_fac, fill = all_statuses)) +
# use geom_bar to add bar plot layer with stacked bars
geom_bar()
美学必须是长度 1 或与数据相同 (11859):填充 - 我不断收到的错误 任何帮助将不胜感激
我会管一切:
library(ggplot2)
library(dplyr)
library(forcats)
storms %>%
mutate(month_fac = factor(month.abb[month], levels = month.abb), ### as you did
order = case_when(status == "hurricane" ~ 1,
status == "tropical storm" ~ 2,
status == "tropical depression" ~ 3), ### create order
all_statuses = fct_reorder(status, order)) %>% ### create factor
ggplot(aes(x = month_fac, fill = all_statuses)) +
geom_bar()
输出为:
请注意,没有二月和三月的风暴数据。您需要为此添加空行。