0

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!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
horseyguy
  • 29,455
  • 20
  • 103
  • 145
  • Always provide an example data and show us what you've done so far. – Arun Mar 11 '13 at 14:53
  • Has your account been hacked?! I'm sorry, but at 8.7K rep you should know better than to post what is essentially "can you do this for me", without posting code of any kind. If you are not familiar with R and need help posting a reproducible example [this question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) may be of great use. – Simon O'Hanlon Mar 11 '13 at 14:57
  • @SimonO101 I'm simply new to R and the plotting/graphing APIs are very confusing, nonetheless, the data is here: https://gist.github.com/anonymous/ee3e76d87c188191fd44 – horseyguy Mar 11 '13 at 14:58
  • @banister, so why couldn't you just paste it to make your question complete? I suggest you edit your question *with the data* (not the link) and include the graph you expect (instead of linking, again). It would be complete enough, even though there's no code from your part. – Arun Mar 11 '13 at 15:00
  • 1
    I thought you may be unfamiliar with `R` which is why I posted the link for you. I would have thought that the principles in that question apply to other languages and posting on SO in general though. The account being hacked comment was meant firmly *tounge-in-cheek* btw, so I hope you didn't take too much offence! – Simon O'Hanlon Mar 11 '13 at 15:21

3 Answers3

4

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

Arun
  • 116,683
  • 26
  • 284
  • 387
3

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

EDi
  • 13,160
  • 2
  • 48
  • 57
2

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

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485