8

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).

Community
  • 1
  • 1
Minnow
  • 1,733
  • 2
  • 26
  • 52

3 Answers3

5

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`.

Thomas K
  • 3,242
  • 15
  • 29
  • Thanks for these. Ultimately, I'd like the plots to be filled and intersecting area to be shaded and the other colors to remain as is. I've updated the question to clarify the point. Any ideas how to make that happen? – Minnow Nov 02 '15 at 18:14
  • @Minnow I think it could be possible by computing the segments separately and then combining them with `geom_segment`, although that seems a bit complicated. In any way it's above my capabilities and time :/ – Thomas K Nov 02 '15 at 19:07
2

I came up with a kludge for this using the concept of layers. In essence I lay down the red without the alpha, add back the blue layer underneath, and then put the red back again with the alpha adjustment to keep the overlapping region at the contrast I want (i.e. it stays purple).

one <- rnorm(mean=10, n = 1000)
two <- rnorm(mean=11, n = 1000)
hist(one, col='blue', main='Bright colors, visible overlap')
hist(two, col='red', add=T)
hist(one, col='blue', add=T)
hist(two, col=rgb(1,0,0,0.5), add=T)

enter image description here

Also works for ggplot:

qplot(one, fill=I('blue'))+
  geom_histogram(aes(two), fill=I('red'))+
  geom_histogram(aes(one), fill=I('blue'))+
  geom_histogram(aes(two), fill=I('red'), alpha=I(0.5))

enter image description here

Minnow
  • 1,733
  • 2
  • 26
  • 52
2

If you do not insist on the overlap, then you may consider plotting the bars of the histogram side-by-side using the "dodge" positioning option of ggplot. For example:

# generate data, some Normal and Gamma variates with the same mean & SD
set.seed(137)
rd <- data.frame(
  n=rnorm(1000, mean=6.0, sd=4.243), 
  g=rgamma(1000, shape=2, scale=3)
)

# convert the data frame to "tall" format
tall.rd <- stack(rd)

# make the plot
ggplot(tall.rd) + geom_histogram(
  aes(values,..density..,colour=ind, fill=ind),
  bins=20,position="dodge"
)

dodged histograms

András Aszódi
  • 8,948
  • 5
  • 48
  • 51
  • 1
    To the anonymous downvoters: an explanation would be much appreciated so that me and others can learn from them. I suspect the problem was that, as I said in the beginning, "if you don't insist on the overlap...", whereas the OP asked for overlapping histograms. Thomas K also provided solutions that did not exactly match this requirement. Moreover, there's nothing inherently wrong with plotting histograms together this way. Or if there is, please explain why. – András Aszódi Jul 04 '17 at 14:17