R 在ggplot中,如何在同一窗口中绘制多个图形?
我希望重新创建以下图表: 我一辈子都搞不懂如何把图表放在彼此之上 任何帮助都将不胜感激 以下是两个机场的数据示例:R 在ggplot中,如何在同一窗口中绘制多个图形?,r,ggplot2,R,Ggplot2,我希望重新创建以下图表: 我一辈子都搞不懂如何把图表放在彼此之上 任何帮助都将不胜感激 以下是两个机场的数据示例: 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",
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")
下面应该可以,我已经将您的示例输入存储为名为“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))
可能是
facets
?你试过什么吗?给我们看看你的代码。刻面是每个不错的ggplot2教程的一部分。谢谢你的评论。诚然,由于不知道从何处开始,我没有尝试过任何其他方法:ggplot(df,aes(收入组,百分比))+geom_条(stat=“identity”,position=“dodge”)
。现在我有了一个起点,我将尝试facets
谢谢,这就是它。我现在想知道如何重新排列收入群体。对此有什么想法吗?请看这个问题: