mjs
mjs

Reputation: 250

How to plot multiple histograms for two factors

I have a data.frame like this (simplified, in fact there are more values per parameter):

   params        value status
1    par1 9.214378e-01   good
...
5    par1 3.907796e+03   good
6    par1 1.440000e-01   bad
...
13   par1 5.343397e+01   bad
14   par2 3.430469e-03   good
...
18   par2 5.722368e-01   good
19   par2 3.764936e-03   bad
...
26   par2 1.291550e-01   bad
27   par3 4.750810e-01   good

with values for 20 parameters for two factors each 'good'/'bad'. I would like to plot it as a set of diagrams in tabular form where I can see differences 'good' versus 'bad', two overlapping histograms for each parameter. Each parameter lives in its characteristic interval.

I have tried with ggplot

p1 <- ggplot(data = DF) +
  geom_histogram(aes(x=value, color=status)) +
  facet_wrap(~params)

but that doesn't work - the bars are stacked one on the other enter image description here

Upvotes: 0

Views: 361

Answers (2)

Adamik Ewert
Adamik Ewert

Reputation: 119

Use position = 'dodge', otherwise the bars will be stack.

I guess you are looking for:

ggplot(data = data, aes(x=value, fill=status)) +
           geom_histogram(position = 'dodge') +
           facet_wrap(~params)

Please make your question more specific, i.e. it's not 2 factors, but one with 2 levels.

Upvotes: 1

mjs
mjs

Reputation: 250

I think I found the solution, among others scales = "free", was missing

p1 <- ggplot(data = DF) +
  geom_histogram(aes(x=value, fill=as.factor(status))) +
  facet_wrap(~params,scales = "free")

The plots looks now, for 10 parameters only, enter image description here

Upvotes: 0

Related Questions