I want to get a proportional stacked area graph like in here: https://r-graphics.org/recipe-line-graph-proportional-stacked-area
I do the following, even forcing the columne value into numeric:
this is the code:
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")
result:
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))
dput(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, 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")
CodePudding user response:
The problem is that your Year
variable is in character format. If you change it to a numeric format, you will get your desired output:
library(reshape2)
library(ggplot2)
mabar <- melt(ma, id.vars = c("Year"),
measure.vars = c("ValueWW", "ValueUS"))
ggplot(mabar, aes(x = as.numeric(Year), y = value, fill = variable))
geom_area(position = "fill", colour = "black", size = .2, alpha = .4)
scale_fill_brewer(palette = "Blues")
labs(x = 'Year')
Created on 2022-11-25 with reprex v2.0.2
Data used
Please note, I had to put your dput
through online OCR to convert the image to text, then tidy it by hand. It is much easier to select the output as text, copy it, and paste it into your question as text:
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))