如何将 ggplot facet_grid() 添加到条形图中?

How do i add ggplot facet_grid() into a barchart?

我对 R 和一般编码还很陌生 我有一个与此类似的数据框,但行数更多:

weekofmonth <- c('week1','week2','week3','week4','week','week2','week3','week4','week1','week2','week3','week4','week1','week2','week3','week4','week1','week2','week3','week4')
arrival_delayed <- c('Yes','No','No','Yes','Yes','No','No','No','Yes','Yes','No','Yes','No','Yes','No','Yes','No','Yes','No','Yes')
quarters <- c('Q1','Q2','Q3','Q4','Q2','Q3','Q4','Q1','Q3','Q4','Q1','Q2','Q3','Q4','Q1','Q2','Q3','Q4','Q1','Q2')

data<- data.frame(weekofmonth,arrival_delayed,quarters)

我已经成功地为数据 $quarter == 'Q1' 创建了一个堆叠条形图,其中 x 轴是 'weeks of the month',y 轴是 'percentage delayed'.

我使用了 ggplot2 和 dplyr 包。

weeks_1stQuarter <-data  %>%
  filter(quarters == 'Q1')%>%
  mutate(weekofmonth = factor(x= weekofmonth, levels = c('week1','week2','week3','week4'), labels = c('1st Week','2nd Week','3rd Week','4th Week'))) %>%
  count(weekofmonth, arrival_delayed) %>%       
  group_by(weekofmonth) %>%
  mutate(pct= prop.table(n) * 100) %>%
  ggplot() + 
  aes(weekofmonth, pct, fill=arrival_delayed) +
  geom_bar(stat="identity") +
  ylab("Percentage of Flights") +
  xlab("Week of the Month") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%")),position=position_stack(vjust=0.5)) +
  ggtitle("percentage delay per week of the month (1st Quarter)") +
  coord_flip()+
  theme_bw()

plot(weeks_1stQuarter)

但是,我正在尝试将 facet_grid() 添加到代码中以分别显示 Q1、Q2、Q3 和 Q4 的 4 个不同图表。

这是我想出的:

weeks_all_Quarters <-data  %>%
  mutate(quarters = factor(x=quarters, levels=c('Q1','Q2','Q3','Q4'), labels=c('1st Quarter','2nd Quarter','3rd Quarter','4th Quarter'))) %>%
  mutate(weekofmonth = factor(x= weekofmonth, levels = c('week1','week2','week3','week4'), labels = c('1st Week','2nd Week','3rd Week','4th Week'))) %>%
  count(weekofmonth, arrival_delayed) %>%       
  group_by(weekofmonth) %>%
  mutate(pct= prop.table(n) * 100) %>%
  ggplot() + 
  aes(weekofmonth, pct, fill=arrival_delayed) +
  facet_grid(.~quarters)+
  geom_bar(stat="identity") +
  ylab("Percentage of Flights") +
  xlab("Week of the Month") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%")),position=position_stack(vjust=0.5)) +
  ggtitle("percentage delay per week of the month (1st Quarter)") +
  coord_flip()+
  theme_bw()

plot(weeks_all_Quarters)  

出现错误:

Error: At least one layer must contain all faceting variables: `quarters`.
* Plot is missing `quarters`
* Layer 1 is missing `quarters`
* Layer 2 is missing `quarters`

我不太确定我做错了什么。

提前感谢您的帮助!

问题是您应该在代码中使用 add_count() 代替 count()count(weekofmonth, arrival_delayed) 行生成的数据框包含这两列的计数(新列 = n),但 删除所有其他列 - 包括“四分之一” ".

使用 add_count() 将添加计数列 ("n"),但不会删除其他列。

如果您从原始代码输出数据框,您会看到不同之处:

data  %>%
  mutate(quarters = factor(x=quarters, levels=c('Q1','Q2','Q3','Q4'), labels=c('1st Quarter','2nd Quarter','3rd Quarter','4th Quarter'))) %>%
  mutate(weekofmonth = factor(x= weekofmonth, levels = c('week1','week2','week3','week4'), labels = c('1st Week','2nd Week','3rd Week','4th Week'))) %>%
  count(weekofmonth, arrival_delayed)

add_count()相比:

data  %>%
  mutate(quarters = factor(x=quarters, levels=c('Q1','Q2','Q3','Q4'), labels=c('1st Quarter','2nd Quarter','3rd Quarter','4th Quarter'))) %>%
  mutate(weekofmonth = factor(x= weekofmonth, levels = c('week1','week2','week3','week4'), labels = c('1st Week','2nd Week','3rd Week','4th Week'))) %>%
  add_count(weekofmonth, arrival_delayed)

如果您进行更改,结果图如下所示: