如何在同一页面展示尺寸一致的多幅ggplot柱状图?
问题解决:统一ggplot图表尺寸并完整显示数据
问题描述
尝试在同一页面展示3组数据相似、单位相近的柱状图,但当前输出存在以下问题:
- 图表尺寸不一致
- 部分内容被截断(如x轴标签、柱状图顶部)
- 布局混乱
当前输出效果:
问题根源分析
- Y轴范围设置错误:所有图表在
labs()里错误设置了ylim = c(0, .8),但数据值为几百到上千,直接导致柱状图顶部被截断 - 布局未对齐:
ggarrange布局时未设置对齐参数,3个图表放在2行2列的布局中未做适配 - X轴标签未处理:过长的X轴标签未换行,导致显示不全
- 冗余代码:
scale_fill_grey()未实际生效(已在geom_bar中指定fill="gray")
修改后的完整代码
library(ggplot2) library(ggpubr) library(stringr) # 用水行为数据 WaterBehavCount <- data.frame( Behavior=c("Limit household water use", "Engage in group activities to protect water quality", "Choose fewer polluting products", "Install water efficient taps or aerators", "Install low flow shower heads", "Install a front loader washing machine", "Install a water efficient dishwasher", "Installed a rainwater tank"), Participants=c(1142, 271, 905, 817, 938, 744, 684, 481) ) # 用水行为柱状图 WaterBehavBar <- ggplot(WaterBehavCount, aes(x=reorder(Behavior, -Participants), y=Participants)) + geom_bar(stat="identity", fill="gray") + labs(x = "用水行为", y = "参与人数") + scale_y_continuous(limits = c(0, 1600)) + # 统一Y轴范围,适配最大数据值 scale_x_discrete(labels = function(x) str_wrap(x, width = 12)) + # X轴标签换行 theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.text.x = element_text(size = 9, angle = 90, hjust = 1, vjust = 0.5), axis.title=element_text(size=10), legend.position="none") # 能源行为数据 EnergyBehavCount <- data.frame( Behavior=c("Turn the lights off when leaving a room", "Limit the use of heaters/air conditioners", "Wash clothes using cold water instead of warm/hot water", "Switch off standby mode of appliances or electronic devices ", "Air dry laundry instead of using a clothes dryer", "Use low emission forms of transport (bus, bike, etc.)", "Engage in activities to promote renewable energy", "Install energy efficient appliances", "Install low-energy light bulbs", " Install thermal insulation for walls or roofs", "Install solar panels for electricity and/or hot water", "Install batteries to store solar energy "), Participants=c(1462, 1236, 1226, 909, 1310, 575, 498, 1040, 1230, 834, 523, 147) ) # 能源行为柱状图 EnergyBehavBar <- ggplot(EnergyBehavCount, aes(x=reorder(Behavior, -Participants), y=Participants)) + geom_bar(stat="identity", fill="gray") + labs(x = "能源行为", y = "参与人数") + scale_y_continuous(limits = c(0, 1600)) + scale_x_discrete(labels = function(x) str_wrap(x, width = 12)) + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.text.x = element_text(size = 9, angle = 90, hjust = 1, vjust = 0.5), axis.title=element_text(size=10), legend.position="none") # 生物多样性行为数据 BioBehavCount <- data.frame( Behavior=c("Plant a native plant in the garden", "Provide water sources to native wildlife", "Put up a nest box for wildlife", "Engage in organized tree planting", "Participate in wildlife monitoring programs", "Sign a petition to protect native wildlife ", "Engage in native vegetation restoration projects"), Participants=c(1110, 1063, 497, 499, 351, 612, 437) ) # 生物多样性行为柱状图 BioBehavBar <- ggplot(BioBehavCount, aes(x=reorder(Behavior, -Participants), y=Participants)) + geom_bar(stat="identity", fill="gray") + labs(x = "生物多样性保护行为", y = "参与人数") + scale_y_continuous(limits = c(0, 1600)) + scale_x_discrete(labels = function(x) str_wrap(x, width = 12)) + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"), axis.text.x = element_text(size = 9, angle = 90, hjust = 1, vjust = 0.5), axis.title=element_text(size=10), legend.position="none") # 组合图表:统一尺寸、对齐布局 ggarrange(WaterBehavBar, EnergyBehavBar, BioBehavBar, labels = c("A", "B", "C"), ncol = 2, nrow = 2, align = "hv", # 水平+垂直对齐 heights = c(1, 1), # 统一行高 widths = c(1, 1)) # 统一列宽
关键修改说明
- 修正Y轴范围:用
scale_y_continuous(limits = c(0, 1600))替代错误的labs(ylim=...),确保所有柱状图完整显示 - X轴标签换行:通过
str_wrap()自动拆分过长的标签文本,避免截断 - 统一布局:添加
align="hv"实现图表对齐,设置heights和widths保证尺寸一致 - 清理冗余代码:移除未生效的
scale_fill_grey()
内容的提问来源于stack exchange,提问作者Jaimi Mostellar
相关产品推荐
相关产品推荐

