R语言 堆积面积图

mi7gmzs6  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(117)

我尝试使用下面的数据构建一个堆叠条形图,如下图所示

structure(list(fyear = c(1973L, 1973L, 1973L, 1973L, 1973L, 1973L, 
1973L, 1973L, 1973L, 1973L, 1974L, 1974L, 1974L, 1974L, 1974L, 
1974L, 1974L, 1974L, 1974L, 1974L, 1975L, 1975L, 1975L, 1975L, 
1975L, 1975L, 1975L, 1975L, 1975L, 1975L, 1976L, 1976L, 1976L, 
1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1976L, 1977L, 1977L, 
1977L, 1977L, 1977L, 1977L, 1977L, 1977L, 1977L, 1977L, 1978L, 
1978L, 1978L, 1978L, 1978L, 1978L, 1978L, 1978L, 1978L, 1978L, 
1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
1979L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 
1980L, 1980L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 
1981L, 1981L, 1981L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 
1982L, 1982L, 1982L, 1982L, 1983L, 1983L, 1983L, 1983L, 1983L, 
1983L, 1983L, 1983L, 1983L, 1983L, 1984L, 1984L, 1984L, 1984L, 
1984L, 1984L, 1984L, 1984L, 1984L, 1984L, 1985L, 1985L, 1985L, 
1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1986L, 1986L, 
1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1986L, 1987L, 
1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 1987L, 
1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 1988L, 
1988L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 1989L, 
1989L, 1989L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 1990L, 
1990L, 1990L, 1990L, 1991L, 1991L, 1991L, 1991L, 1991L, 1991L, 
1991L, 1991L, 1991L, 1991L, 1992L, 1992L, 1992L, 1992L, 1992L, 
1992L, 1992L, 1992L, 1992L, 1992L, 1993L, 1993L, 1993L, 1993L, 
1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1994L, 1994L, 1994L, 
1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1996L, 
1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 
1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 
1997L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 
1998L, 1998L, 1999L, 1999L, 1999L, 1999L, 1999L, 1999L, 1999L, 
1999L, 1999L, 1999L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 
2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L), decile = c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1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L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L), aqc = c(8.026, 25.264, 36.707, 49.425, 62.967, 180.801, 
146.152, 375.081, 459.262, 1793.79, 2.138, 5.487, 19.307, 40.773, 
40.553, 78.561, 131.67, 240.01, 519.041, 2642.667, 2.714, 5.012, 
11.51, 44.909, 38.113, 56.121, 89.896, 175.831, 395.009, 1649.218, 
3.234, 5.063, 15.012, 35.267, 69.047, 97.13, 160.346, 435.601, 
595.292, 3297.715, 2.091, 9.002, 26.751, 20.589, 57.39, 147.81, 
259.983, 452.414, 909.704, 4483.252, 1.217, 8.286, 18.734, 27.799, 
64.927, 138.785, 254.652, 511.244, 1450.376, 6906.012, 5.085, 
6.03, 19.215, 33.668, 83.464, 130.216, 301.328, 589.243, 1998.429, 
15238.431, 4.71, 11.223, 26.146, 52.282, 100.987, 78.166, 287.448, 
695.313, 2140.437, 12509.352, 4.185, 6.09, 34.696, 74.211, 91.562, 
238.599, 280.406, 581.371, 1742.864, 23167.399, 5.707, 16.487, 
32.586, 62.297, 131.6, 272.814, 367.051, 914.362, 2901.78, 21291.306, 
5.833, 22.197, 55.618, 107.386, 171.241, 298.681, 626.501, 1218.896, 
3116.716, 17301.998, 5.222, 19.313, 46.938, 97.905, 225.105, 
410.427, 812.545, 1467.933, 5028.846, 47616.07, 8.858, 15.198, 
32.587, 153.197, 170.153, 432.657, 679.242, 1622.068, 7303.593, 
61972.19, 4.608, 22.012, 42.914, 156.015, 239.228, 567.487, 1456.551, 
2288.666, 11410.184, 66466.258, 7.142, 22.413, 46.805, 145.324, 
327.731, 650.071, 1319.851, 3473.202, 12280.044, 53261.775, 4.369, 
8.109, 35.323, 68.339, 307.668, 696.009, 1461.702, 3143.923, 
11201.534, 75823.44, 2.781, 11.107, 46.214, 106.846, 254.917, 
564.505, 1142.906, 4022.812, 13304.968, 71507.63, 2.142, 19.543, 
40.732, 73.738, 162.933, 591.713, 920.969, 2267.103, 5290.651, 
52960.243, 2.821, 15.946, 40.153, 85.214, 142.618, 357.457, 612.184, 
2171.455, 3922.209, 18179.535, 6.895, 19.711, 62.967, 201.738, 
292.3, 604.185, 1250.571, 2759.223, 5322.491, 27973.899, 5.76, 
24.901, 121.936, 220.282, 239.432, 818.512, 1635.272, 3330.754, 
6867.998, 38666.737, 2.772, 25.368, 109.093, 233.234, 533.904, 
1356.889, 2198.26, 5562.666, 11700.899, 65170.509, 8.948, 46.987, 
128.075, 223.661, 515.63, 1352.104, 2442.165, 5837.748, 14690.197, 
89438.934, 14.841, 76.208, 226.953, 573.719, 887.612, 1562.334, 
3151.308, 10575.403, 21333.252, 87786.485, 11.47, 53.115, 212.696, 
533.147, 1191.303, 2565.67, 5349.15, 11237.233, 26483.158, 96679.065, 
4.719, 47.41, 206.085, 534.106, 1472.352, 2996.582, 6938.636, 
13015.445, 39119.59, 166710.367, 21.361, 43.316, 169.951, 437.661, 
1099.548, 2604.792, 6457.597, 10853.591, 30745.062, 195928.725, 
10.237, 55.028, 140.733, 581.656, 778.381, 2478.812, 3644.332, 
9875.783, 25549.944, 209993.458, 2.311, 6.231, 119.484, 297.157, 
835.23, 1692.454, 3995.753, 7563.021, 22086.533, 125996.803, 
9.267, 16.501, 69.258, 254.275, 744.021, 1757.302, 3722.03, 8664.169, 
17488.136, 110572.461, 2.889, 26.828, 66.396, 218.282, 773.109, 
1777.349, 4960.932, 11154.74, 21400.441, 91990.725, 4.557, 56.822, 
158.786, 408.695, 1238.617, 3116.524, 7428.101, 17442.901, 36581.834, 
160145.727, 8.364, 109.322, 341.981, 630.376, 1413.697, 3426.555, 
8990.226, 18082.069, 34750.918, 170331.944, 0.024, 77.212, 311.221, 
640.407, 1771.889, 4674.726, 9836.698, 21579.81, 40553.496, 265797.233, 
13.816, 45.277, 251.717, 934.718, 1977.421, 5487.372, 11792.534, 
19557.977, 56374.607, 282188.805, 9.341, 57.505, 246.47, 665.058, 
1939.893, 4579.739, 8454.289, 16584.646, 36336.147, 210633.253, 
-32.476, 27.212, 139.352, 449.952, 779.702, 2250.401, 3922.821, 
7759.439, 14132.049, 155946.989, 3.089, -3.987, 223.515, 401.423, 
1891.905, 3777.697, 5638.534, 19983.479, 31149.976, 159308.152, 
4.818, 11.503, 126.814, 646.115, 1728.084, 4669.634, 11805.327, 
24444.57, 56399.228, 195090.65, 4.701, 15.32, 187.211, 542.416, 
1427.483, 4636.275, 10087.896, 23421.155, 60777.504, 184594.331, 
4.266, 28.946, 92.289, 479.74, 1449.198, 3895.78, 12080.537, 
22905.5, 43948.277, 159979.725, 1.455, 55.356, 218.478, 890.68, 
2168.553, 7362.743, 17683.152, 30864.759, 69018.437, 186734.088, 
3.813, 59.82, 256.472, 1028.549, 2340.785, 7642.471, 17226.897, 
28096.815, 55790.942, 311346.357, 5.352, -1.477, 149.102, 721.958, 
1669.592, 5201.385, 16059.146, 24484.214, 74952.492, 352411.98, 
5.771, 45.862, 227.157, 509.451, 2159.67, 7424.075, 16746.456, 
19523.876, 63376.754, 257371.667, -5.427, 71.904, 209.999, 954.592, 
3355.693, 6465.466, 16282.521, 28672.971, 63497.656, 391754.129, 
12.277, 25.385, 245.962, 816.115, 3544.49, 6704.624, 17687.8, 
31557.577, 63588.006, 236190.991)), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -470L), groups = structure(list(
    fyear = 1973:2019, .rows = structure(list(1:10, 11:20, 21:30, 
        31:40, 41:50, 51:60, 61:70, 71:80, 81:90, 91:100, 101:110, 
        111:120, 121:130, 131:140, 141:150, 151:160, 161:170, 
        171:180, 181:190, 191:200, 201:210, 211:220, 221:230, 
        231:240, 241:250, 251:260, 261:270, 271:280, 281:290, 
        291:300, 301:310, 311:320, 321:330, 331:340, 341:350, 
        351:360, 361:370, 371:380, 381:390, 391:400, 401:410, 
        411:420, 421:430, 431:440, 441:450, 451:460, 461:470), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -47L), .drop = TRUE))

对于图中的图表,我应用了类似于以下的方法,但不知何故,它对新数据不起作用:

ggplot(deciles, aes(x = fyear, y = aqc, group=decile)) +
  geom_area(alpha=0.6, size=1, colour="black")

然后我决定用实际的百分比作为一列和一个检查列来修改我的DF,以再次检查我的代码是否正常。2我想要的是每年100%(不确定我的代码是否显示)
一个二个一个一个
我还是得不到我需要的...

n8ghc7c1

n8ghc7c11#

你可以先用group_by来计算每组的百分比,确保你的fill美学是factor,如果你想有百分比标签,你可以用scale_y_continuous来使用scales包的percent

library(dplyr)
library(ggplot2)
df %>%
  group_by(fyear, decile) %>%
  summarise(n = sum(aqc)) %>%
  mutate(pct = n/sum(n)) %>%
  ggplot(aes(x = fyear, y = pct, fill = factor(decile))) +
  geom_area(alpha = 0.6, size = 1, colour = "black") +
  labs(fill = "deciles") +
  scale_y_continuous(labels = scales::percent)

创建于2023年1月20日,使用reprex v2.0.2
使用ifelse将负值替换为零:

library(dplyr)
library(ggplot2)
df %>%
  group_by(fyear, decile) %>%
  summarise(n = sum(aqc)) %>%
  mutate(pct = n/sum(n)) %>%
  mutate(pct = ifelse(pct < 0, 0, pct)) %>%
  ggplot(aes(x = fyear, y = pct, fill = factor(decile))) +
  geom_area(alpha = 0.6, size = 1, colour = "black") +
  labs(fill = "deciles") +
  scale_y_continuous(labels = scales::percent)

输出:

相关问题