Stacked area chart

Asked

Viewed 103 times

4

I’m trying to make an area chart according to the image below: inserir a descrição da imagem aqui

But I’m using a different database than the one used for this graph and I’m having some questions:

1- I am using the following code:

ggplot(novo, aes(x = Commissioned, y = somaacumulada)) +
  theme_bw()+
  geom_area(position = position_dodge(width = 0),alpha=0.8, aes(fill=Country)) +
  scale_x_date(breaks= "10 years", labels = date_format("%Y"))+
  scale_y_continuous(breaks=seq(0,37000,1000)) +
  xlab('Ano') +ylab('Potência Instalada [MW]') 

The result I’m getting with this code is as follows: inserir a descrição da imagem aqui

You can see that there is no area piling going on, but I don’t know how to do it in the code. One thing I thought would be the fact that the country does not have data for every year, for example Austria has the somaacumulada for 2017, but not for 2000.

2- I would also like to limit the X axis to show the values only between the years 1900 and 2020, I tried to enter the code

coord_cartesian(xlim = c(as.Date('01-01-1900'), as.Date('01-01-2020')))

However, an empty chart is displayed.

3- How do I change the color of the chart areas? For example, if I want to choose the color of each country separately.

4- Database

"Rated Power","Country","Commissioned","d","m","a","somaacumulada"
1.14,"Resto do mundo",1985-01-01,1,1,1985,17451.5
1.28,"Japan",1995-01-01,1,1,1995,16969.78
1.7,"Resto do mundo",1966-01-01,1,1,1966,2542.3
2,"China",1992-11-01,1,11,1992,485
3.5,"Germany",1973-01-01,1,1,1973,1346.7
3.6,"Spain",1955-01-01,1,1,1955,10.8
4.6,"Resto do mundo",1957-01-01,1,1,1957,238.6
7,"Austria",1969-01-01,1,1,1969,352
7.2,"Spain",1929-01-01,1,1,1929,7.2
8.5,"United States",1954-01-01,1,1,1954,37.5
11,"France",1951-01-01,1,1,1951,11
11.3,"Spain",2014-06-27,27,6,2014,6983.7
12,"India",1976-01-01,1,1,1976,162
13.5,"Japan",1961-01-01,1,1,1961,13.5
14,"Spain",1966-01-01,1,1,1966,564.8
15.9,"Resto do mundo",1969-01-01,1,1,1969,2758.2
17,"Resto do mundo",1986-01-01,1,1,1986,18791.5
22,"China",1973-11-01,1,11,1973,63
24,"Resto do mundo",1953-01-01,1,1,1953,234
25.2,"United States",1973-01-01,1,1,1973,6518.7
28,"United States",1972-01-01,1,1,1972,5277.5
29,"United States",1929-01-01,1,1,1929,29
30,"Resto do mundo",1937-01-01,1,1,1937,165
30.8,"Resto do mundo",1989-01-01,1,1,1989,20912.3
35,"Germany",1959-01-01,1,1,1959,404
36.5,"Italy",1965-01-01,1,1,1965,489.7
37,"Resto do mundo",2016-12-31,31,12,2016,33017.7
40,"Resto do mundo",1974-01-01,1,1,1974,5482.2
40,"United States",2012-09-14,14,9,2012,22560.7
41,"China",1968-05-01,1,5,1968,41
42,"Italy",1968-01-01,1,1,1968,646.7
45,"Resto do mundo",1944-01-01,1,1,1944,210
45,"United States",1994-01-01,1,1,1994,21425.7
48,"France",1957-01-01,1,1,1957,59
49,"Germany",1983-01-01,1,1,1983,3999.7
49.2,"Germany",1960-01-01,1,1,1960,453.2
50,"Austria",2011-01-01,1,1,2011,3200
50,"United States",1971-08-01,1,8,1971,5249.5
53,"Resto do mundo",1990-01-01,1,1,1990,20965.3
54,"Spain",1982-01-01,1,1,1982,2786.3
58,"United States",1984-01-01,1,1,1984,15300.9
60,"Resto do mundo",1972-01-01,1,1,1972,3817.2
68,"Resto do mundo",1964-01-01,1,1,1964,1004.6
70,"China",2000-12-01,1,12,2000,1605
80,"China",2000-01-01,1,1,2000,1535
80,"China",1997-12-01,1,12,1997,655
80,"Germany",1968-01-01,1,1,1968,1203.2
80,"Italy",1960-01-01,1,1,1960,316
80,"Italy",1905-04-11,11,4,1905,80
80,"Resto do mundo",1977-01-01,1,1,1977,7542.2
82.8,"Resto do mundo",1997-01-01,1,1,1997,25361.1
88.5,"Spain",1969-01-01,1,1,1969,785.3
90,"China",1997-05-01,1,5,1997,575
90,"Germany",1964-01-01,1,1,1964,763.2
90,"Spain",1971-01-01,1,1,1971,1685.3
97,"United States",1972-12-31,31,12,1972,6493.5
99,"Germany",1955-01-01,1,1,1955,249
102,"China",2002-06-01,1,6,2002,5943
108,"Austria",1981-01-01,1,1,1981,2125
111,"Spain",1982-01-01,1,1,1982,2732.3
113,"Austria",1956-01-01,1,1,1956,113
115,"Italy",1966-01-01,1,1,1966,604.7
120,"Austria",1979-01-01,1,1,1979,1728
120,"China",2005-09-01,1,9,2005,6063
120,"Germany",1958-10-15,15,10,1958,369
125,"Resto do mundo",1972-01-01,1,1,1972,3942.2
132,"Spain",1966-01-01,1,1,1966,696.8
133,"Spain",1978-01-01,1,1,1978,2621.3
134,"Italy",1980-01-01,1,1,1980,3128.7
135,"Resto do mundo",1937-01-01,1,1,1937,135
137,"Japan",1961-01-01,1,1,1961,150.5
137.2,"Italy",1964-01-01,1,1,1964,453.2
140,"Austria",2011-06-01,1,6,2011,3150
140,"Germany",1969-01-01,1,1,1969,1343.2
140,"Germany",2010-04-20,20,4,2010,5352.7
140,"Resto do mundo",1988-01-01,1,1,1988,20481.5
143,"Resto do mundo",1981-01-19,19,1,1981,10669.2
145,"Resto do mundo",2016-12-31,31,12,2016,32980.7
150,"Germany",1951-01-01,1,1,1951,150
150,"India",1951-01-01,1,1,1951,150
153,"Germany",1989-08-08,8,8,1989,4152.7
156,"Italy",1949-01-01,1,1,1949,236
156,"Resto do mundo",1971-01-01,1,1,1971,3154.2
158,"Italy",2005-11-01,1,11,2005,7070.7
160,"Resto do mundo",1977-01-01,1,1,1977,7462.2
160,"Resto do mundo",1988-01-01,1,1,1988,20341.5
164,"Germany",1976-01-01,1,1,1976,1990.7
164,"Resto do mundo",1958-01-01,1,1,1958,402.6
171,"Resto do mundo",1996-01-01,1,1,1996,24244.3
174,"Resto do mundo",1958-01-01,1,1,1958,576.6
180,"Resto do mundo",1979-08-08,8,8,1979,10526.2
185,"Resto do mundo",2010-04-01,1,4,2010,29566.7
185,"United States",1956-05-24,24,5,1956,222.5
191.6,"Resto do mundo",2005-01-01,1,1,2005,29381.7
198,"Resto do mundo",1975-01-01,1,1,1975,6272.2
199.8,"United States",1987-01-01,1,1,1987,20315.7
200,"Japan",2014-10-01,1,10,2014,24541.78
200,"Japan",1974-11-01,1,11,1974,4712.5
200,"Japan",2015-11-01,1,11,2015,25141.78
200,"Resto do mundo",1968-01-01,1,1,1968,2742.3
200,"United States",1981-01-01,1,1,1981,15242.9
208,"United States",1979-12-01,1,12,1979,15042.9
210,"Italy",1971-01-01,1,1,1971,856.7
211,"Resto do mundo",2015-01-01,1,1,2015,31817.7
215,"Spain",1973-01-01,1,1,1973,1900.3
219.1,"Spain",1985-01-01,1,1,1985,3365.4
220,"Germany",1964-01-01,1,1,1964,673.2
220,"Japan",1968-01-01,1,1,1968,1023.5
224,"Resto do mundo",1984-07-06,6,7,1984,15222.36
225,"Spain",1964-01-01,1,1,1964,235.8
228,"Spain",1975-01-01,1,1,1975,2128.3
231,"Austria",1972-01-01,1,1,1972,583
232,"Austria",1958-01-01,1,1,1958,345
240,"France",1966-11-26,26,11,1966,299
240,"India",1998-01-01,1,1,1998,2472
240,"Italy",1978-01-01,1,1,1978,2994.7
240,"Japan",1990-01-01,1,1,1990,16068.5
240,"Resto do mundo",1970-01-01,1,1,1970,2998.2
240,"Resto do mundo",2015-12-01,1,12,2015,32057.7
240,"United States",1961-01-28,28,1,1961,462.5
250,"India",2006-01-01,1,1,2006,2722
250,"United States",1977-01-01,1,1,1977,12671.7
259,"Resto do mundo",2016-12-31,31,12,2016,34012.7
260,"Japan",1970-01-01,1,1,1970,2246.5
260,"United States",1971-01-01,1,1,1971,5199.5
270,"Resto do mundo",1979-01-01,1,1,1979,8262.2
276,"Resto do mundo",1984-01-01,1,1,1984,14998.36
288,"Japan",1976-06-01,1,6,1976,6120.5
289,"Austria",1980-01-01,1,1,1980,2017
292,"Resto do mundo",1974-01-01,1,1,1974,6074.2
295,"Austria",1976-01-01,1,1,1976,878
300,"Resto do mundo",1977-01-01,1,1,1977,6932.2
300,"Resto do mundo",2018-01-01,1,1,2018,36252.7
300,"Resto do mundo",1974-12-31,31,12,1974,5782.2
302,"Resto do mundo",1983-08-31,31,8,1983,14006.36
303,"Japan",1968-01-01,1,1,1968,803.5
310,"France",1975-01-01,1,1,1975,609
314,"United States",1973-01-01,1,1,1973,11821.7
315,"Resto do mundo",1985-01-01,1,1,1985,18774.5
315,"Spain",1964-01-01,1,1,1964,550.8
317,"Japan",1981-02-01,1,2,1981,9383.5
324,"United States",1967-01-01,1,1,1967,3256.5
330,"Italy",1975-01-01,1,1,1975,2754.7
336,"Resto do mundo",1981-01-01,1,1,1981,11740.36
340,"Japan",1969-11-01,1,11,1969,1986.5
350,"Japan",1965-01-01,1,1,1965,500.5
350,"Resto do mundo",1982-01-01,1,1,1982,13704.36
360,"Austria",1988-01-01,1,1,1988,2485
360,"Austria",2018-12-31,31,12,2018,4470
360,"Germany",1967-01-01,1,1,1967,1123.2
360,"Resto do mundo",1975-06-01,1,6,1975,6632.2
360,"Resto do mundo",1963-12-31,31,12,1963,936.6
360,"Spain",1977-01-01,1,1,1977,2488.3
360,"Spain",1982-01-01,1,1,1982,3146.3
370,"Resto do mundo",1977-01-01,1,1,1977,7302.2
384,"Resto do mundo",1996-01-01,1,1,1996,24628.3
400,"India",1988-01-01,1,1,1988,2232
400,"Japan",2014-06-09,9,6,2014,24941.78
400,"Resto do mundo",1988-05-01,1,5,1988,20881.5
400,"United States",1965-01-01,1,1,1965,1302.5
420,"China",1991-09-01,1,9,1991,483
420,"Resto do mundo",1979-11-25,25,11,1979,10346.2
424,"United States",1968-01-01,1,1,1968,4499.5
430,"Austria",2014-01-01,1,1,2014,4110
440,"Resto do mundo",1965-10-15,15,10,1965,2540.6
440,"United States",1963-01-01,1,1,1963,902.5
440,"United States",1970-01-01,1,1,1970,4939.5
445,"France",1983-01-01,1,1,1983,3244
450,"Resto do mundo",1978-01-01,1,1,1978,7992.2
460,"Japan",1981-05-01,1,5,1981,9066.5
466,"Japan",1970-07-01,1,7,1970,2712.5
468,"Spain",1985-01-01,1,1,1985,3833.4
480,"Austria",2011-09-07,7,9,2011,3680
480,"France",1979-01-01,1,1,1979,1889
480,"Germany",1974-01-01,1,1,1974,1826.7
500,"Japan",1975-01-01,1,1,1975,5212.5
500,"Resto do mundo",1979-01-01,1,1,1979,8762.2
500,"Resto do mundo",1984-01-01,1,1,1984,15722.36
500,"Resto do mundo",2002-01-01,1,1,2002,26761.1
511.2,"United States",1978-12-01,1,12,1978,14834.9
520,"Resto do mundo",2004-12-31,31,12,2004,29190.1
525,"Austria",2009-05-01,1,5,2009,3010
560,"United States",1966-03-07,7,3,1966,1862.5
568,"Italy",1973-01-01,1,1,1973,1424.7
600,"Italy",1991-01-01,1,1,1991,5912.7
600,"Japan",1987-01-01,1,1,1987,14778.5
600,"Japan",1996-01-01,1,1,1996,19069.78
600,"United States",1974-01-01,1,1,1974,12421.7
600,"United States",1985-01-01,1,1,1985,17112.9
603,"Resto do mundo",1972-01-01,1,1,1972,3757.2
614,"Resto do mundo",1982-09-23,23,9,1982,13354.36
615,"Japan",1984-01-01,1,1,1984,12978.5
620,"Japan",1976-01-01,1,1,1976,5832.5
623,"Japan",1969-01-01,1,1,1969,1646.5
640,"Resto do mundo",1986-01-01,1,1,1986,19431.5
650,"Resto do mundo",1996-06-20,20,6,1996,25278.3
675,"Japan",1973-06-01,1,6,1973,4512.5
709,"Resto do mundo",2004-05-01,1,5,2004,28670.1
710,"United States",1973-12-19,19,12,1973,9100.7
716,"Resto do mundo",1983-01-01,1,1,1983,14722.36
720,"Resto do mundo",1991-01-01,1,1,1991,21685.3
730,"Austria",1979-01-01,1,1,1979,1608
735.16,"Resto do mundo",1981-01-01,1,1,1981,11404.36
736,"Resto do mundo",2016-12-31,31,12,2016,33753.7
748,"France",1986-01-01,1,1,1986,3992
750,"Resto do mundo",1986-02-14,14,2,1986,20181.5
778,"Resto do mundo",2016-12-31,31,12,2016,32835.7
780,"Japan",1981-02-01,1,2,1981,10163.5
788,"Resto do mundo",1995-01-01,1,1,1995,24073.3
800,"China",1997-06-01,1,6,1997,1455
800,"France",1976-01-01,1,1,1976,1409
810,"Spain",1970-01-01,1,1,1970,1595.3
819,"United States",1967-01-01,1,1,1967,4075.5
900,"India",2008-02-01,1,2,2008,5072
900,"Japan",1994-01-01,1,1,1994,16968.5
900,"Resto do mundo",2017-01-01,1,1,2017,34912.7
900,"Resto do mundo",2000-12-19,19,12,2000,26261.1
910,"France",1982-12-01,1,12,1982,2799
910,"Germany",1976-01-01,1,1,1976,2900.7
940,"Japan",2012-01-01,1,1,2012,24341.78
1000,"China",2009-02-01,1,2,2009,14487
1000,"China",2008-12-01,1,12,2008,11087
1000,"China",2011-08-14,14,8,2011,16687
1000,"China",2006-12-20,20,12,2006,8863
1000,"India",2016-12-31,31,12,2016,6072
1000,"Italy",1973-01-01,1,1,1973,2424.7
1000,"Italy",1985-01-01,1,1,1985,5312.7
1000,"Italy",1991-01-01,1,1,1991,6912.7
1000,"Japan",1982-01-01,1,1,1982,12363.5
1000,"Resto do mundo",2015-01-01,1,1,2015,31606.7
1000,"Resto do mundo",1981-05-01,1,5,1981,12740.36
1008,"Resto do mundo",1985-08-01,1,8,1985,18459.5
1040,"Resto do mundo",2013-01-01,1,1,2013,30606.7
1040,"Resto do mundo",2018-01-01,1,1,2018,35952.7
1050,"Germany",1979-01-01,1,1,1979,3950.7
1050,"Japan",1988-07-01,1,7,1988,15828.5
1060,"Germany",2004-10-20,20,10,2004,5212.7
1065,"United States",1991-01-01,1,1,1991,21380.7
1070,"United States",1966-01-01,1,1,1966,2932.5
1095,"United States",1995-01-01,1,1,1995,22520.7
1096,"Resto do mundo",1964-04-17,17,4,1964,2100.6
1119,"United States",1972-01-01,1,1,1972,6396.5
1125,"Japan",1972-01-01,1,1,1972,3837.5
1139,"Spain",1986-01-01,1,1,1986,4972.4
1160,"United States",1973-07-01,1,7,1973,10260.7
1164,"Resto do mundo",1979-01-01,1,1,1979,9926.2
1184,"Italy",1981-01-01,1,1,1981,4312.7
1200,"China",2010-10-01,1,10,2010,15687
1200,"China",2011-12-15,15,12,2011,17887
1200,"China",2009-10-16,16,10,2009,12287
1200,"China",2013-12-20,20,12,2013,22735
1200,"China",2009-11-21,21,11,2009,13487
1200,"China",2012-09-29,29,9,2012,21535
1200,"Japan",1982-01-01,1,1,1982,11363.5
1200,"Japan",1996-04-01,1,4,1996,20269.78
1200,"Japan",1986-07-01,1,7,1986,14178.5
1200,"Japan",2011-07-01,1,7,2011,23401.78
1200,"Resto do mundo",2003-11-05,5,11,2003,27961.1
1206,"Japan",1980-01-01,1,1,1980,8606.5
1212,"United States",1984-06-30,30,6,1984,16512.9
1224,"China",2006-01-01,1,1,2006,10087
1224,"China",2014-11-20,20,11,2014,23959
1247,"United States",1973-01-01,1,1,1973,11507.7
1280,"Japan",1980-05-01,1,5,1980,7400.5
1450,"India",2006-06-20,20,6,2006,4172
1500,"China",2015-01-01,1,1,2015,25459
1500,"Japan",1995-11-01,1,11,1995,18469.78
1500,"Resto do mundo",1973-01-01,1,1,1973,5442.2
1600,"Resto do mundo",1994-01-01,1,1,1994,23285.3
1652,"United States",1978-01-01,1,1,1978,14323.7
1670,"India",1981-01-01,1,1,1981,1832
1728,"Resto do mundo",1984-01-01,1,1,1984,17450.36
1800,"China",2005-11-01,1,11,2005,7863
1820,"France",1987-01-01,1,1,1987,5812
1836,"China",2000-12-25,25,12,2000,3441
1872,"United States",1973-01-01,1,1,1973,8390.7
1932,"Japan",1998-06-01,1,6,1998,22201.78
2000,"Spain",2013-10-14,14,10,2013,6972.4
2400,"China",2000-06-01,1,6,2000,5841
2448,"China",2011-06-15,15,6,2011,20335
3003,"United States",1985-12-01,1,12,1985,20115.9
730,"Austria",2019-01-01,1,1,2019,1608
1060,"Germany",2019-01-01,1,1,2019,5212.7
1184,"Italy",2019-01-01,1,1,2019,4312.7
1670,"India",2019-01-01,1,1,2019,1832
1728,"Resto do mundo",2019-01-01,1,1,2019,17450.36
1820,"France",2019-01-01,1,1,2019,5812
1932,"Japan",2019-01-01,1,1,2019,22201.78
2000,"Spain",2019-01-01,1,1,2019,6972.4
2448,"China",2019-01-01,1,1,2019,20335
3003,"United States",2019-01-01,1,1,2019,20115.9
```



  • 1

    I already know the problem, you have what we call Missing implicitos, n has data in all years for all countries

  • I imagined it @Bruno , you know how to solve or maneuver it?

  • 1

    Vc tb has duplicated data in years

  • 1

    Example 1985 in the rest of the world has two lines

  • @Bruno this duplicity happens due to the date of the Commissioned column have in two periods of the same year, however I am not using the year on the x axis, use of the full date of the Commissioned column

  • 1

    maybe these two dates complicate my chart arrives at 150k but it gets ugly, it was barely have with reply hj

  • @Bruno is around this number (150k) that I calculated that I would give. Vlw for manifesting!

  • 1

    I am not able to reproduce with the data you have passed, but the solution goes through to exchange the argument position of geom_area of "dodge" for "stack"

  • @Tomásbarcellos When I remove the position = position_dodge(width = 0) it gets a totally wrong graph

Show 4 more comments

1 answer

3


As pointed out in the comments, there are two problems in their data: 1) not all dates have values for all countries (implicit ones); 2) more than one value for the same country on the same date.

Solution:

  • convert your data.frame to wide format and then again to long format so that all dates are present for all countries
  • do this using a function that aggregates duplicate values
  • fill the missing values between dates with the value of the last filled cell (or with a linear interpolation, etc)
  • convert Nas at the beginning of each country’s series to 0

I’m using the package data table. as a matter of personal preference. The same can be done with tidyverse or with reshape2 more basic R functions (approx, is.na, etc.).

library(ggplot2)
library(data.table)

# Carrega os dados como data.table
novo <- fread("dados.csv")
# pode usar setDT(novo) para converter de data.frame para data.table

# Converte para formato largo, agregando valores duplicados e preenchendo os ausentes com NA
gdados <- dcast(novo,
                Commissioned ~ Country,
                value.var = "somaacumulada",
                fun = mean, # ou outra função mais relevante para seus dados
                fill = NA)
# ou reshape2::dcast ou tidyr::spread
# pode usar também a ~ Country para ter linhas mais suaves, como no seu gráfico de exemplo

# Converte de volta para formato longo
gdados <- melt(gdados,
               id.vars = "Commissioned",
               variable.name = "Country",
               value.name = "somaacumulada")
# ou reshape2::melt ou tidyr::gather

# Converte datas para formato Date
gdados[, Commissioned := as.Date(Commissioned)]

# Ordena os dados por país e data
setorder(gdados, Country, Commissioned)

# Preenche os NAs subsequentes com o último valor presente
gdados[, acum := nafill(somaacumulada, type = "locf"), by = Country]
# nafill faz parte do pacote data.table; verifique ?approx se estiver usando apenas o pacote base

# Converte os NAs no início das séries para 0
gdados[, acum := nafill(acum, fill = 0), by = Country]
# ou gdados[is.na(gdados)] <- 0 se usar o pacote base

ggplot(gdados, aes(Commissioned, acum, fill = Country)) +
  geom_area() +
  scale_x_date(date_breaks = "10 years",
              date_labels = "%Y",
              limits = c(as.Date("1939-01-01"), max(gdados$Commissioned))) +
  labs(x = NULL, y = 'Potência Instalada [MW]', fill = NULL) +
  theme_minimal()

inserir a descrição da imagem aqui

  • Thanks for the answer, already saved my day! In the code I noticed that Limits, in the " scale_x_date", starts in 1939, but in the plotting is starting in 1935, could you change that? You asked because I’m trying to limit the chart to the year 1929 and at most until the year 2019 using this property and I’m getting the same result as you

  • 1

    By default ggplot expands the limits of the plotting area, you can eliminate this with scale_x_date(..., expand = c(0,0))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.