3
I am building a graph indicating the population of the Brazilian states, organized by regions, according to the code below:
State <- c("Rondônia", "Acre", "Amazonas", "Roraima", "Pará", "Amapá", "Tocantins",
"Maranhão", "Piauí", "Ceará", "Rio Grande do Norte", "Paraíba", "Pernambuco", "Alagoas", "Sergipe", "Bahia",
"Minas Gerais", "Espírito Santo", "Rio de Janeiro", "São Paulo",
"Paraná", "Santa Catarina", "Rio Grande do Sul",
"Mato Grosso do Sul", "Mato Grosso", "Goiás", "Distrito Federal" )
Population <- c(1805788, 829619, 4063614, 522636, 8366628, 797722, 1550194,
7000229, 3219257, 9020460, 3507003, 4025558, 9473266, 3375823, 2288116, 15344447,
21119536, 4016356, 16718956, 45094866,
11320892, 7001161, 11322895,
2713147, 3344544, 6778772, 3039444)
Region <- c(rep("Região Norte", 7),
rep("Região Nordeste", 9),
rep("Região Sudeste", 4),
rep("Região Sul", 3),
rep("Região Centro-Oeste", 4))
dfPop <- data.frame(State, Population, Region)
ggplot(data=dfPop,
aes(x=Region, weights=Population / 1E+6)) +
geom_bar(aes(fill=State), color="Black") +
geom_text(aes(x=Region, y=Population / 1E+6, group=State, label=State),
position = position_stack(vjust = 0.5), size=3.3) +
guides(fill=FALSE) +
xlab("Região do Brasil") + ylab("Milhões de habitantes")
The resulting graph is as follows::
I have two problems that I would like to solve and I’m not getting
1. Hide label of states with less than 3 million inhabitants
To clarify the graph, I want to hide the label of states with less than 3 million inhabitants. For this I found a tip for filtering the data.frame directly in the element geom_text, in order to remove these states, as follows:
ggplot(data=dfPop,
aes(x=Region, weights=Population / 1E+6)) +
geom_bar(aes(fill=State), color="Black") +
geom_text(data=dfPop[dfPop$Population > 3E+6,],
aes(x=Region, y=Population / 1E+6, group=State, label=State),
position = position_stack(vjust = 0.5), size=3.3) +
guides(fill=FALSE) +
xlab("Região do Brasil") + ylab("Milhões de habitantes")
However, as you can see, all other labels have been misplaced. How could I hide the desired labels without displacing the others?
2. Order stacking based on population of states (most populous below)
As an alternative to solving problem 1, I sought to sort each stack according to the state’s population, to hide the name of the states at the top of the stack. However, even ordering the input date.frame, I couldn’t do this visual ordering in ggplot. Can anyone help me?
Thanks for the support!