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

求助:使用R绘制比例堆叠面积图出现异常

比例堆叠面积图绘制异常问题排查

问题说明

想要绘制比例堆叠面积图,已执行数据融合、数值型转换等操作,但最终生成的图表存在异常。以下是详细的执行代码、绘制结果及数据集信息。

执行代码

mabar <- melt(ma, id.vars = c("Year"),
              measure.vars = c("ValueWW", "ValueUS"))

mabar$value <- as.numeric(as.character(mabar$value))


ggplot(mabar, aes(x = Year, y = value, fill = variable)) +
  geom_area(position = "fill", colour = "black", size = .2, alpha = .4) +
  scale_fill_brewer(palette = "Blues")

绘制结果

异常的比例堆叠面积图

数据集dput输出

原数据集ma

structure(list(Year = c("1985", "1986", "1987", "1988", "1989", 
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", 
"1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", 
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
"2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"
), NumberWW = c(2676, 4228, 5279, 7440, 10135, 10814, 14722, 
14102, 14772, 16816, 20278, 24310, 26227, 30218, 33132, 39783, 
31047, 27201, 29573, 32953, 36025, 41407, 47455, 45173, 40710, 
44844, 43976, 41480, 39568, 43847, 48052, 49991, 53302, 50607, 
49327, 44926, 52000), ValueWW = c(347, 435, 506, 777, 758, 540, 
397, 400, 516, 624, 1039, 1217, 1824, 2678, 4116, 3623, 1866, 
1242, 1411, 2145, 2794, 4023, 4920, 3075, 2187, 2750, 2668, 2533, 
2536, 3960, 4779, 3646, 3777, 3393.563, 3370.106, 2817.39, 4898
), NumberUS = c(2309, 3447, 3708, 4443, 5840, 5982, 5702, 5915, 
6782, 8076, 9368, 11856, 13147, 14780, 13245, 14114, 9652, 8571, 
9272, 10744, 11436, 13019, 13999, 11731, 9466, 10191, 10536, 
10629, 10877, 12283, 12885, 13430, 15558, 14936, 17759, 15271, 
21000), ValueUS = c(305.64, 353.54, 373.17, 586.05, 466.09, 254.16, 
176.99, 185.13, 317.61, 414.7, 666.58, 750.39, 1116.22, 1816.41, 
2138.18, 1965.81, 1010.58, 520.54, 668.86, 1006.42, 1342.1, 1843.89, 
1967.06, 1215.09, 877.61, 981.8, 1247.04, 995.65, 1214.79, 2153.8, 
2417.39, 1784.77, 1761.54, 1931.81, 1887.57, 1125.82, 2587), 
    GDPUS = c(4579.6325, 4855.21625, 5236.438, 5641.5795, 5963.1445, 
    6158.12925, 6520.32725, 6858.5585, 7287.2365, 7639.74925, 
    8073.12175, 8577.5525, 9062.81675, 9631.17175, 10250.952, 
    10581.929, 10929.10825, 11456.4495, 12217.19575, 13039.197, 
    13815.583, 14474.227, 14769.8615, 14478.06675, 15048.97, 
    15599.73175, 16253.97, 16843.19575, 17550.68775, 18206.0235, 
    18695.10575, 19477.3365, 20533.0575, 21380.976, 21060.47425, 
    23315.08125, 23315.08125)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -37L))

处理后数据集mabar

structure(list(Year = c("1985", "1986", "1987", "1988", "1989", 
"1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", 
"1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", 
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", 
"2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", 
"1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", 
"1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", 
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", 
"2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", 
"2017", "2018", "2019", "2020", "2021"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("ValueWW", "ValueUS"
), class = "factor"), value = c(347, 435, 506, 777, 758, 540, 
397, 400, 516, 624, 1039, 1217, 1824, 2678, 4116, 3623, 1866, 
1242, 1411, 2145, 2794, 4023, 4920, 3075, 2187, 2750, 2668, 2533, 
2536, 3960, 4779, 3646, 3777, 3393.563, 3370.106, 2817.39, 4898, 
305.64, 353.54, 373.17, 586.05, 466.09, 254.16, 176.99, 185.13, 
317.61, 414.7, 666.58, 750.39, 1116.22, 1816.41, 2138.18, 1965.81, 
1010.58, 520.54, 668.86, 1006.42, 1342.1, 1843.89, 1967.06, 1215.09, 
877.61, 981.8, 1247.04, 995.65, 1214.79, 2153.8, 2417.39, 1784.77, 
1761.54, 1931.81, 1887.57, 1125.82, 2587)), row.names = c(NA, 
-74L), class = "data.frame")

问题原因与解决方法

核心原因

图表异常的关键在于Year列是字符型,ggplot会将其识别为离散变量,导致x轴年份并非连续数值,堆叠面积的计算和绘制逻辑因此出错。

解决代码

将Year转换为数值型,让x轴成为连续变量,重新绘制图表:

# 转换Year为数值型
mabar$Year <- as.numeric(mabar$Year)

ggplot(mabar, aes(x = Year, y = value, fill = variable)) +
  geom_area(position = "fill", colour = "black", size = .2, alpha = .4) +
  scale_fill_brewer(palette = "Blues") +
  # 可选:优化x轴刻度,每5年显示一个刻度
  scale_x_continuous(breaks = seq(1985, 2021, 5))

修改后即可得到正常的比例堆叠面积图,x轴年份按连续顺序排列,堆叠区域能准确展示各年份的比例关系。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 20:15:33