Home > Software design >  Creating a dual bar chart
Creating a dual bar chart

Time:03-02

I'm looking to create a bar char similar to the example below, although i don't know the name or code used for this. Im looking to get a horizontal bar char with PM2.5 to the left and life expectancy to the right of my continents labeled down the middle. My data and example of what i want it to look like below.

Image of what i want my graph to look like: enter image description here

CodePudding user response:

This should get you started:

## please make sample data availabe, e.g. by pasting the output of `dput(df)`
life_bar <- structure(list(cty = c("Africa", "Americas", "Eastern Mediterranean", 
"Europe", "South-East Asia", "Western Pacific"), val = c(62.7, 
74.8, 71.3, 78.1, 71.6, 74.3)), class = "data.frame", row.names = c(NA, 
6L))

structure(list(cty = c("Africa", "Americas", "Eastern Mediterranean", 
"Europe", "South-East Asia", "Western Pacific"), val = c(41.1, 
19.5, 47, 15.6, 37.7, 16.4)), class = "data.frame", row.names = c(NA, 
6L))

## the actual code example:
life_bar %>%
    ggplot()   
    geom_col(aes(cty, val), fill = NA, colour = 'blue')  
    geom_col(data = air_pollution_bar, aes(cty,val), fill = 'red')   
    scale_x_discrete(limits = rev)   ## reverse country axis
    coord_flip() ## make horizontal bars

So, technically it's rather simple, if(!) you want to display data with apperently different units along the same axis ;-)

  • Related