1

I'm struggling with the following issue:

I want to plot two histograms, but since the statistics of one of the two classes is much less than the other I need to add a second y-axis to allow a direct comparison of the values.

I report below the code I used at the moment and the result.

Thank you in advance!


ggplot(data,aes(x= x ,group=class,fill=class)) + geom_histogram(position="identity",
  alpha=0.5, bins = 20)+ theme_bw() 

Plot

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
GBen
  • 15
  • 1
  • 5
  • [This previous so post](https://stackoverflow.com/questions/26917689/how-to-use-facets-with-a-dual-y-axis-ggplot/40746716#40746716) could answer your question. – dario Feb 11 '20 at 12:25
  • Maybe you should be plotting densities instead of counts? – camille Feb 11 '20 at 13:22

2 Answers2

2

Consider the following situation where you have 800 versus 200 observations:

library(ggplot2)

df <- data.frame(
  x = rnorm(1000, rep(c(1, 2), c(800, 200))),
  class = rep(c("A", "B"), c(800, 200))
)

ggplot(df, aes(x, fill = class)) +
  geom_histogram(bins = 20, position = "identity", alpha = 0.5,
  # Note that y = stat(count) is the default behaviour
                 mapping = aes(y = stat(count)))

enter image description here

You could scale the counts for each group to a maximum of 1 by using y = stat(ncount):

ggplot(df, aes(x, fill = class)) +
  geom_histogram(bins = 20, position = "identity", alpha = 0.5,
                 mapping = aes(y = stat(ncount)))

enter image description here

Alternatively, you can set y = stat(density) to have the total area integrate to 1.

ggplot(df, aes(x, fill = class)) +
  geom_histogram(bins = 20, position = "identity", alpha = 0.5,
                 mapping = aes(y = stat(density)))

enter image description here

Note that after ggplot 3.3.0 stat() probably will get replaced by after_stat().

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • thank you master, that was the solution! Simple and fast, I don't know how I missed it. You helped me a lot. Have a nice day! =) – GBen Feb 12 '20 at 09:30
0

How about comparing them side by side with facets?

ggplot(data,aes(x= x ,group=class,fill=class)) +
  geom_histogram(position="identity",
                 alpha=0.5,
                 bins = 20) +
  theme_bw() +
  facet_wrap(~class, scales = "free_y")
Aron Strandberg
  • 3,040
  • 9
  • 15
  • Thank you for your suggestion. However, since I have to repeat the same histogram for 6 sets I'm trying to summarize both the histograms in one chart. Anyway, if I won't be able to find a way, I'm going to follow your suggestion. Best. – GBen Feb 11 '20 at 14:00