0

I have posted this question another time and gave one answer but when I did again I did not give same output. I have a data frame named mydata which contains population for different country.

df<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "male", 
population =c(180,160,130,140,150,160,170,90,85,80,75,70,65,60,40), country = 1)
                
df1<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "female", 
population =c(160,150,120,130,140,150,160,80,75,70,65,60,55,50,30),country  = 1)

df2<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "male", 
population =c(80,77,66,65,69,69,54,50,44,40,38,29,20,12,8), country = 2)

df3<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "female",
population =c(76,70,61,63,60,51,41,39,37,33,30,28,23,22,10), country = 2)

df4<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "male", 
 population =c(29,27,30,26,24,22,20,18,16,14,12,10,8,6,4), country = 3)

df5<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "female",
population =c(28,24,20,22,23,24,22,21,20,18,16,13,12,10,6), country = 3)


mydata<- rbind(df,df1,df2,df3,df4,df5)

in the code below, I got a population pyramid for three countries. because of different number of population in each country, my population pyramids are different visually and not comparable, how can I change y axis so that I have a comparable population pyramid?

ggplot(data=mydata, aes(x=age, y = ifelse(gender == "male", -population, population), fill=gender)) + 
  geom_col() + 
  facet_wrap(~country) +
  coord_flip()

enter image description here

mehmo
  • 409
  • 2
  • 8

1 Answers1

1

As @Stefan suggests, adding scales = "free_x" to the facet_wrap allows each x-axis to freely scale (depending on the data).

library(ggplot2)
ggplot(data=mydata, aes(x=age, y = ifelse(gender == "male", -population, population), fill=gender)) + 
  geom_col() + 
  facet_wrap(~country, scales = "free_x") +
  coord_flip()

enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • thanks @ Andy Brown is there any way ? – mehmo Jun 24 '21 at 04:22
  • What is your expected output? It's not clear from your question. – AndrewGB Jun 24 '21 at 04:42
  • as you suggested me an animated population pyramid, I want to add your code to that code. when I do that the form of animating change. – mehmo Jun 24 '21 at 04:59
  • `pop <- ggplot(data = pop1, aes( x = age1, y = ifelse(gender == 1,-mean, mean), fill = gender, group = district )) + geom_col() + #facet_wrap( ~ district, scales = "free_x") + coord_flip() + transition_states(district, transition_length = 2, state_length = 1) + labs(title = 'district: {closest_state}')` – mehmo Jun 24 '21 at 05:00
  • when I did your suggested code in animating population pyramid, the form of animating has changed. – mehmo Jun 24 '21 at 05:06
  • I don't think that is possible with `gganimate`. If you are wanting them to be comparable, then I recommend doing some kind of standardization (e.g., n per 100,000 people) on the population for each country and across age groups, then they would be more comparable along the x-axis. – AndrewGB Jun 24 '21 at 15:53