Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to show overlapping histograms in R without adjusting transparency?

The objective is to show overlapping histograms, but I want to avoid using the alpha adjustment so that the colours remain bright.

Is there a way to do this without adjusting the alpha arg?

Goal is to display the colors shown below:

hist(rnorm(mean=10, n = 1000), col='blue')
hist(rnorm(mean=11, n = 1000), col='red', add=T)

enter image description here

But also show the overlapping area as shown here

hist(rnorm(mean=10, n = 1000), col='blue')
hist(rnorm(mean=11, n = 1000), col=rgb(1,0,0,0.5), add=T)

enter image description here

Similar question that doesn't quite address transparency:

How to create black and white transparent overlapping histograms using ggplot2?

I'd be fine with densities and use of other graphing packages (e.g. lattice, ggplot2, etc).

Edit: I'd like the plots to be filled and the intersecting area to be a different color (e.g. purple where red and blue intersect).

like image 520
Minnow Avatar asked Nov 02 '15 14:11

Minnow


People also ask

How do you overlay two histograms in R?

Plot two histograms Using plot() will simply plot the histogram as if you'd typed hist() from the start. However, you can now use add = TRUE as a parameter, which allows a second histogram to be plotted on the same chart/axis.

How do I change the transparency of a histogram in R?

To set transparency level, use lessR function set or use rgb function directly. Color of the border of the bars. To set transparency level, use function set or use rgb function directly.

How do you show multiple histograms on the same axes?

In order to create two histograms on the same plot, similar to the PLOTYY function, you need to create overlapping axes and then plot each histogram on one axis. 1.In order to create overlapping axes, simply create one axes and then create another axes with the same position as the first.

Can histograms overlap?

An Overlay Histogram allows you to visualize and compare multiple Populations superimposed on each other.


1 Answers

A solution using ggplot2 and geom_density.

library(ggplot2)
library(tidyr)

# create data
set.seed(1234)
df <- data.frame(x = rnorm(1000, 10), y = rnorm(1000, 11)) %>% 
  gather(key, value) # use tidyr::gather to convert from wide to long format

ggplot(df, aes(value, colour = key)) +
  geom_density(show.legend = F) +
  theme_minimal() +
  scale_color_manual(values = c(x = "red", y = "blue"))

# use 'adjust' to adjust density estimation
ggplot(df, aes(value, colour = key)) +
  geom_density(show.legend = F, adjust = .5) +
  theme_minimal() +
  scale_color_manual(values = c(x = "red", y = "blue"))

Histogram

Since alpha is no option, apart from using densities you could stack the histograms on top of each other, although I'd prefer densities, since they are easier to compare.

# using stacked histograms
ggplot(df, aes(value, fill = key)) +
  geom_histogram(show.legend = F) +
  theme_minimal() +
  scale_fill_manual(values = c(x = "red", y = "blue"))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

like image 58
Thomas K Avatar answered Oct 05 '22 23:10

Thomas K