Question

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!

Was it helpful?

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

OTHER TIPS

First you should merge both data.frame based on country. The you could use for example ggplot2 for plotting.

Here is an example using ggplot2:

# some data
male_heights <- data.frame(country = LETTERS[1:20],
                           heights = runif(20, 10,20))
female_heights <- data.frame(country = LETTERS[1:20],
                            heights = runif(20, 10,20))

# merge both data.frame
df_m <- merge(male_heights, female_heights, by = 'country', suffixes=c('_males', '_females'))

# bring data to long format
require(reshape2)
dfm <- melt(df_m)

# plot
ggplot(dfm, aes(x = country, y = value, fill = variable)) +
  geom_bar(stat = 'identity', position = 'dodge')

enter image description here

For the sake of completeness, here are some other options available, one in base R, and one with the "lattice" package that is usually installed along with R. Using @Arun's sample data, here is a basic example of each. (There are lots of ways to customize the appearance of each.)

## Base R -- Nothing fancy. 
## Requires a vector or matrix for your data
barplot(xtabs(height ~ grp + country, dd), 
        beside = TRUE, col = c("red", "blue"))

enter image description here

## lattice -- can work directly with your data.frame
## Several interesting options for groupings
library(lattice)
barchart(height ~ country, data = dd, groups = grp, 
         ylim = c(0, max(dd$height)+10), col = c("red", "blue"))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top