Reputation: 8940
I would like to plot two or more histograms from the same data, but inverting the coordinates of one group, like the following:
(note: this figure is taken from Grossman et al 2011, A composite of Multiple Signals Distinguishes Causal Variants in Regions of Positive Selection)
For example, let's take the diamonds dataset from ggplot2:
> library(ggplot2)
> head(diamonds)
carat cut color clarity depth table price x y z
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
One approach I have tried has been to calculate the histogram using stat_bin without plotting it, changing the values of the counts column returned by it.
> my_sb <-stat_bin(data=diamonds, mapping=aes(x=x)
The documentation of stat_bin says that this function should return a data.frame equal to the mapped one, but adding four new columns (count, density, ncount, ndensity). However, I can not find these columns anywhere:
# I supposed that this should contain a count, density columns, but it does not.
> print(head(my_sb$data))
carat cut color clarity depth table price x y z
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
Another possible approach is to use scale_y_reverse(), but I don't know how to apply it to a single dataset.
The third approach that I can think of is to use viewPorts, but I am not quite sure on how to implement it.
Upvotes: 4
Views: 2013
Reputation: 173737
Like this maybe:
ggplot(data = diamonds) +
geom_histogram(aes(x = x,y = ..count..)) +
geom_histogram(aes(x = x,y = -..count..))
FYI - I couldn't remember exactly how I'd done this in the past, so I Googled "ggplot2 inverted histogram" and clicked on the first hit, a StackOverflow question.
I'm not sure exactly how the proto object that stat_bin
returns is structured, but the new variables are in there somewhere. The way this works is that geom_histogram
itself calls stat_bin
to perform the binning, and so it has access to the computed variables, which we can map to the y
variable.
Upvotes: 5