-1
I am using the following code to try to plot a graph
ggplot(final2, aes(x = final2$a, y =final2$somaacumulada,fill=final2$Country)) +
theme_bw()+
geom_area(position = position_dodge(width = 0.8))
This is a part of data frame
"Rated Power","Country","Commissioned","d","m","a","somaacumulada"
1.14,"Resto do mundo","01.01.1985",1,1,1985,17451.5
1.28,"Japan","01.01.1995",1,1,1995,16969.78
1.7,"Resto do mundo","01.01.1966",1,1,1966,2542.3
2,"China","01.11.1992",1,11,1992,485
3.5,"Germany","01.01.1973",1,1,1973,1346.7
3.6,"Spain","01.01.1955",1,1,1955,10.8
4.6,"Resto do mundo","01.01.1957",1,1,1957,238.6
7,"Austria","01.01.1969",1,1,1969,352
7.2,"Spain","01.01.1929",1,1,1929,7.2
8.5,"United States","01.01.1954",1,1,1954,37.5
11,"France","01.01.1951",1,1,1951,11
11.3,"Spain","27.06.2014",27,6,2014,6758.7
12,"India","01.01.1976",1,1,1976,162
13.5,"Japan","01.01.1961",1,1,1961,13.5
14,"Spain","01.01.1966",1,1,1966,339.8
15.9,"Resto do mundo","01.01.1969",1,1,1969,2758.2
17,"Resto do mundo","01.01.1986",1,1,1986,18791.5
22,"China","01.11.1973",1,11,1973,63
24,"Resto do mundo","01.01.1953",1,1,1953,234
25.2,"United States","01.01.1973",1,1,1973,6518.7
28,"United States","01.01.1972",1,1,1972,5277.5
29,"United States","01.01.1929",1,1,1929,29
30,"Resto do mundo","01.01.1937",1,1,1937,165
30.8,"Resto do mundo","01.01.1989",1,1,1989,20912.3
35,"Germany","01.01.1959",1,1,1959,404
36.5,"Italy","01.01.1965",1,1,1965,489.7
37,"Resto do mundo","31.12.2016",31,12,2016,33017.7
40,"Resto do mundo","01.01.1974",1,1,1974,5482.2
40,"United States","14.09.2012",14,9,2012,22560.7
41,"China","01.05.1968",1,5,1968,41
42,"Italy","01.01.1968",1,1,1968,646.7
45,"Resto do mundo","01.01.1944",1,1,1944,210
45,"United States","01.01.1994",1,1,1994,21425.7
48,"France","01.01.1957",1,1,1957,59
49,"Germany","01.01.1983",1,1,1983,3999.7
49.2,"Germany","01.01.1960",1,1,1960,453.2
50,"Austria","01.01.2011",1,1,2011,3200
50,"United States","01.08.1971",1,8,1971,5249.5
53,"Resto do mundo","01.01.1990",1,1,1990,20965.3
54,"Spain","01.01.1982",1,1,1982,2561.3
58,"United States","01.01.1984",1,1,1984,15300.9
However I am not able to make the order of the areas right, the larger areas that are in front of the graph and hide the smaller ones that are behind, someone would know a way to solve this?
Another question I have would be to limit the values on the X axis to have a range that better represents the graph.
EDIT --
I made the following editions:
final2$Country <- factor(final2$Country, levels = c('Resto do mundo','United States','Japan','China','Italy','Germany','Spain','France','India','Austria'))
ggplot(final2, aes(x = Commissioned, y =somaacumulada)) +
theme_bw()+
geom_area(position = position_dodge(width = 0.8),alpha=1, 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]')
And I got the following result
however the result is not suitable, still has some hidden values and, as I am using accumulated sum, the values of any country should not decrease from one year to another, or increases or remains constant.
In
aes(...)
doesn’t needfinal2$
. Remove and try again.– Rui Barradas
Always try to leave a playable example, so you can receive a response. The area overlay problem, try to sort the data with
arrange
or equivalent. For the x-axis limit, trycoord_cartesian(xlim = c(limite inferior, limite superior))
.– Alexandre Sanches
@Alexandresanches Or filter the zeros, by the appearance of the graph there are zeros until after 1950.
– Rui Barradas
you can change the opacity through alpha
– Bruno
@Alexandresanches I didn’t understand what you meant by the reproducible example. I thank you for the limit code, it worked perfectly! There are no zeroes after 1950, what happens is that some data remains constant until 2019.
– Matheus Dias
@Bruno Thanks! I was using the opacity to be able to see the hidden data, but in their precise final product with opacity 0
– Matheus Dias
Always try to post a reproducible example so we can replicate your data and come up with solutions. For example, you posted your code, but I can’t test it in my version of R because I don’t have the data and you didn’t put any example data.
– Alexandre Sanches
@Ruibarradas I did what you said and I got the same result.
– Matheus Dias
@Alexandresanches I entered a part of the data
– Matheus Dias