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

ggplot2堆叠面积图图例含多余,1字符及趋势异常问题求助

ggplot2堆叠面积图:图例异常与趋势反转修复

问题现象

  1. 图例中每个组分名称后出现多余的,1字符,无法仅显示组分名称
  2. 堆叠面积趋势不符合实际:Cone、Needle、Bark、Branch组分随DBH增大显示为占比上升,实际应为下降;仅Stem组分占比应随DBH增大上升

用户提供的样本数据与代码

样本数据

样本数据

原代码

Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")

DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)

plotly::ggplotly(
  ggplot(DRProportions[order(DRProportions$Component),], aes(x = DBH, y = Percentage, fill = Component)) +
    geom_area(alpha=0.6 , linewidth=.5, colour="white") +
    scale_colour_manual("", values = c(Cone="deepskyblue", Needle="darkslategray", Bark="darkseagreen", Branch="deeppink2", Stem="darksalmon"))+  
    labs(y="Percentage (%)", x="DBH (cm)")+  
    theme(axis.text = element_text(size = 14))+
    theme(axis.title = element_text(size = 15))+
    theme(legend.text = element_text(size = 15)))

错误图表

错误图表

修复方案与代码

1. 图例,1字符修复

问题根源:原代码用scale_colour_manual设置颜色,但实际是对fill(堆叠面积的填充色)做映射,导致ggplot同时生成fill和colour两个图例,合并时出现,1后缀。

解决方法:把scale_colour_manual替换为scale_fill_manual,专门针对填充色设置颜色映射。

2. 趋势反转修复

问题根源:堆叠面积图默认按因子水平顺序从下到上堆叠,原顺序将占比上升的Stem放在最上层,导致下方其他组分看起来随DBH增大而面积上升。

解决方法:调整堆叠顺序,让Stem在最底层(占比上升时向上扩展),其他下降组分在上方,两种实现方式如下:

方式一:调整因子水平顺序

将Stem设为第一个因子水平,其余组分按原顺序反转,确保堆叠时Stem在最底层:

# 调整因子顺序,Stem放在最底层,其余组分按原顺序反转
Component_order <- c("Stem", "Branch", "Bark", "Needle", "Cone")
colours_order <- c("darksalmon", "deeppink2", "darkseagreen", "darkslategrey", "deepskyblue")

DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)

plotly::ggplotly(
  ggplot(DRProportions, aes(x = DBH, y = Percentage, fill = Component)) +
    geom_area(alpha = 0.6, linewidth = .5, colour = "white") +
    # 用scale_fill_manual设置填充颜色,匹配调整后的因子顺序
    scale_fill_manual("", values = setNames(colours_order, Component_order)) +  
    labs(y = "Percentage (%)", x = "DBH (cm)") +  
    theme(axis.text = element_text(size = 14),
          axis.title = element_text(size = 15),
          legend.text = element_text(size = 15))
)

方式二:反转堆叠顺序(不修改因子顺序)

在geom_area中添加position = position_stack(reverse = TRUE)参数,直接反转默认堆叠顺序:

Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")

DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)

plotly::ggplotly(
  ggplot(DRProportions, aes(x = DBH, y = Percentage, fill = Component)) +
    # 反转堆叠顺序,让Stem在底层
    geom_area(alpha = 0.6, linewidth = .5, colour = "white", position = position_stack(reverse = TRUE)) +
    scale_fill_manual("", values = setNames(colours_order, Component_order)) +  
    labs(y = "Percentage (%)", x = "DBH (cm)") +  
    theme(axis.text = element_text(size = 14),
          axis.title = element_text(size = 15),
          legend.text = element_text(size = 15))
)

修复说明

  • 图例修复后,将仅显示组分名称,无多余后缀
  • 趋势修复后,Stem组分面积随DBH增大向上扩展(占比上升),其他组分面积随DBH增大向下收缩(占比下降),符合实际数据规律

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 04:47:05