Search code examples
rplotgraphing

How to plot multipleset data in R?


Like in the plot found here I would like to display multiple datasets in the same bar graph.

My data is essentially "male/female" heights for various countries. I want the country along the x-axis and two bar graphs (one blue one red) for male and female heights per country.

I've struggled with this for a few days now, and still haven't figured it out.

Each data set is currently stored in its own dataframe, with "countries" in the first column and "height" in the second. So i have both a male_heights and female_heights data frame.

Thanks!


Solution

  • Here's an example with some dummy data:

    # create some dummy data of two data.frames for male and female
    set.seed(45)
    dd.m <- data.frame(country = sample(letters[1:8]), height=sample(150:200, 8))
    dd.f <- data.frame(country = sample(letters[1:8]), height=sample(130:180, 8))
    
    # create an grp column for each of the above data.frames (M, F -> male, female)
    dd.m$grp <- "M"
    dd.f$grp <- "F"
    
    # merge data together
    dd <- rbind(dd.m, dd.f)
    
    # set levels for grp column - which one should be displayed first within the group
    # here, female followed by male
    dd$grp <- factor(dd$grp, levels=c("F", "M"), ordered=TRUE)
    
    # make sure country is a factor (reorder levels if you have to)
    dd$country <- factor(dd$country)
    
    # plot using ggplot
    require(ggplot2)    
    ggplot(data = dd, aes(x=country)) + 
          geom_bar(aes(weights=height, fill=grp), position="dodge") + 
          scale_fill_brewer(palette = "Set1")
    

    This gives: enter image description here