You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R的ggplot2调整图表格式:重新排列柱状图显示方式

解决方案

要实现每个站点对应的Wall和Restored柱子相邻排列,你需要调整ggplot的分组逻辑并移除分面设置,具体修改如下:

修改后的代码

ggplot(sed_avg_long, aes(x = Site, y = Avg_Percentage, fill = Sediment_Type, color = Site_Type, group = Site_Type))+
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), alpha = 0.8) +
  labs(x = "Site", y = "Average Percent Sediment", fill = "Sediment type", color = "Site Type") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_fill_brewer(palette="Set1") +
  scale_color_brewer(palette="Dark2")

关键修改说明

  • 移除分面设置:删掉facet_grid(~Site_Type, scales = "fixed"),分面会将不同Site_Type的内容分隔到独立面板,无法实现同一站点下的柱子相邻。
  • 添加分组规则:在aes中加入group = Site_Type,让ggplot识别需要按站点类型对柱子进行分组排列。
  • 设置柱子错开排列:给geom_bar添加position = position_dodge(width = 0.8),让同一站点下的Wall和Restored柱子横向相邻,width参数可根据需求调整间距。
  • 增加类型区分:添加color = Site_Type并配置对应的颜色比例尺,通过柱子边框颜色区分Wall和Restored类型,同时保留fill = Sediment_Type展示沉积物类型的堆叠占比。

内容的提问来源于stack exchange,提问作者Gabe Veltri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:22:52