0

Hi I have the following columns in a table:

Person
Score1
Score2

and I can plot a historgram of the scores using qplot:

qplot(Score1,data = dat,geom="histogram")

which is fine, - but how do I plot two histograms side by side - one with Score1 and one with Score2.

I have also managed to plot both on the same graph using ggplot and melting Score1 and Score2:

m <- melt(dat[,c(2,3)])

>head(m)

    variable      value
  1 Score1        50
  2 Score2        70
  3 Score1        45
  4 Score2        30.5
  5 Score1        70
  6 Score2        40

ggplot(m,aes(value)) + geom_bar(binwidth = 1)

however when trying to use facet_wrap()

ggplot(m,aes(value)) + geom_bar(binwidth = 1) + facet_wrap(variable ~ Score_type) 

I get:

Error in layout_base(data,cols,drop = drop)
At least one layer must contain all variables used for facetting

Any ideas? I will post if I solve this myself.Also is it possible to order the x axis by Score1 or Score2?

Thanks!

brucezepplin
  • 9,202
  • 26
  • 76
  • 129
  • i'd rather not have them on the same graph. looks messy! – brucezepplin Sep 09 '13 at 09:35
  • Then take a look at the second linked question. – nograpes Sep 09 '13 at 09:36
  • Pretty sure @nograpes is right. Even w/o data and no pointer to what the poster wanted, it seems the answer lies in the second linked question. – hrbrmstr Sep 09 '13 at 09:42
  • @nograpes - hi thanks, just one thing with the second question is that the variable they are plotting there is in one column. i have two separate columns so the melt function used in a few solutions to that question won't work for me. – brucezepplin Sep 09 '13 at 09:43
  • @brucezepplin The `melt` function will make your two columns into one column, making it easy to plot your histograms side-by-side with `ggplot`. – nograpes Sep 09 '13 at 09:46
  • @brucezepplin But there are other problems with your question that make it difficult to answer. You should read the posting guide, include a reproducible example. and make sure that your question hasn't been answered before. – nograpes Sep 09 '13 at 09:48
  • @nograpes - yes have almost got there with the solutions on the second thread posted. I'll amend my question. – brucezepplin Sep 09 '13 at 10:08

1 Answers1

0

Got it- minor error in the facet_wrap function parameter I passed. It works with:

ggplot(m,aes(value)) + geom_bar(binwidth = 1) + facet_wrap(~variable)
brucezepplin
  • 9,202
  • 26
  • 76
  • 129