Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I used ggplot2 to create two transparent overlapping histograms.

test = data.frame(condition = rep(c("a", "b"), each = 500), value = rep(-1, 1000))
test[1:500,]$value = rnorm(500)
test[501:1000,]$value = rnorm(500) + 2

fig = ggplot(test, aes(x = value, fill = condition)) +
      #scale_fill_grey() +
      geom_histogram(position = "identity", alpha = .5)
fig

The resulting plot looks great, but it's in color. I need a grayscale or black/white plot.

Using "scale_fill_grey()" results in a plot with transparency that is very difficult to "read".

Ideally, I would like a black/white plot that uses texture instead of color, for instance, cross hatching: "///" for one condition, "\\\" for the other condition, resulting in "XXX" when the bars overlap. Is this possible?

like image 453
user2363777 Avatar asked May 08 '13 20:05

user2363777


People also ask

How do I make a histogram transparent in R?

This can be done by using fill="transparent" and color="black" arguments in geom_histogram, we need to use color argument because if we don't use then the borders of the histogram bars will also be removed and this color is not restricted to black color only.

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.

Can you build a histogram using ggplot2?

You can also make histograms by using ggplot2 , “a plotting system for R, based on the grammar of graphics” that was created by Hadley Wickham.

What is geom_histogram in Ggplot?

Histograms ( geom_histogram() ) display the counts with bars; frequency polygons ( geom_freqpoly() ) display the counts with lines. Frequency polygons are more suitable when you want to compare the distribution across the levels of a categorical variable.


1 Answers

How about this (no texturing still)?

fig = ggplot(test, aes(x = value, fill = condition)) +
    geom_histogram(position = "identity", alpha = .8) + 
    scale_fill_manual(values=c("grey20", "grey60")) + theme_bw()
fig

enter image description here

like image 113
Arun Avatar answered Oct 18 '22 19:10

Arun