在ggplot中,如何在同一窗口中绘制多个图形?

时间:2017-06-15 13:40:18

标签: r ggplot2

我希望重新创建以下图表: enter image description here

我无法弄清楚我的生活如何将图表放在彼此之上。

非常感谢任何帮助!

以下是两个机场的数据样本:

structure(list(Income_group = c("Under 5750", "Under 5750", "5750 - 8624", 
"5750 - 8624", "8625 - 11499", "8625 - 11499", "11500 - 14374", 
"11500 - 14374", "14375 - 17249", "14375 - 17249", "17250 - 22999", 
"17250 - 22999", "23000 - 28749", "23000 - 28749", "28750 - 34499", 
"28750 - 34499", "34500 - 40249", "34500 - 40249", "40250 - 45999", 
"40250 - 45999", "46000 - 57499", "46000 - 57499", "57500 - 80499", 
"57500 - 80499", "80500 - 114999", "80500 - 114999", "115000 - 172999", 
"115000 - 172999", "173000 - 229999", "173000 - 229999", "over 230000", 
"over 230000", "Under 5750", "Under 5750", "5750 - 8624", "5750 - 8624", 
"8625 - 11499", "8625 - 11499", "11500 - 14374", "11500 - 14374", 
"14375 - 17249", "14375 - 17249", "17250 - 22999", "17250 - 22999", 
"23000 - 28749", "23000 - 28749", "28750 - 34499", "28750 - 34499", 
"34500 - 40249", "34500 - 40249", "40250 - 45999", "40250 - 45999", 
"46000 - 57499", "46000 - 57499", "57500 - 80499", "57500 - 80499", 
"80500 - 114999", "80500 - 114999", "115000 - 172999", "115000 - 172999", 
"173000 - 229999", "173000 - 229999", "over 230000", "over 230000"
), Trip_Type = c("business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure", 
"business", "leisure", "business", "leisure", "business", "leisure"
), Airport = c("Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", 
"Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", 
"Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", 
"Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", 
"Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", "Gatwick", 
"Gatwick", "Gatwick", "Gatwick", "Heathrow", "Heathrow", "Heathrow", 
"Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", 
"Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", 
"Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", 
"Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow", 
"Heathrow", "Heathrow", "Heathrow", "Heathrow", "Heathrow"), 
    percentage = c(1.1, 4.7, 0.8, 1.1, 1.1, 1.5, 1.1, 2.1, 2.4, 
    5, 2.8, 5.5, 6.6, 7.5, 8.9, 9.6, 12.6, 7.7, 10.7, 8.9, 11.3, 
    11.3, 15.6, 15.9, 13.6, 10.1, 6.8, 5.5, 2.2, 1.5, 2.4, 2.3, 
    0.7, 7.3, 0.5, 2.2, 0.9, 2, 1.1, 3, 1.1, 2.8, 3.1, 5.2, 4.2, 
    7.8, 7.2, 8.8, 8.2, 7.6, 10, 10.1, 12.8, 8.8, 18.3, 12.6, 
    14.2, 9.3, 9.6, 6.8, 3.5, 2.5, 4.6, 3.1)), .Names = c("Income_group", 
"Trip_Type", "Airport", "percentage"), row.names = 33:96, class = "data.frame")

1 个答案:

答案 0 :(得分:3)

以下内容应该有效,我已将您的示例输入存储为名为“dat”的数据框:

library(ggplot2)
library(cowplot)

plt = ggplot(dat, aes(Income_group, percentage)) +
geom_bar(stat="identity") + facet_grid(Airport ~.) + background_grid(major = 'y', minor = "none") +
panel_border()

plt + theme(axis.text.x = element_text(angle = 90, hjust = 1))