I want to overlay two histograms in a base R plot in RMarkdown.
If I run the following code (in an RMarkdown code snippet), this works fine and displays the plot below.
Code
a = rnorm(100, 1)
b = rnorm(100, 3)
h1 = hist(a, plot = F)
h1$density = h1$counts/sum(h1$counts)
h2 = hist(b, plot = F)
h2$density = h2$counts/sum(h2$counts)
{
plot(h1
, freq=F
, col=alpha('green', 0.5)
, xlim=c(-3, 7)
, ylim=c(0,0.35)
, ylab='Proportion'
, xlab=""
, main=""
)
plot(h2
, freq=F
, col=alpha('blue', 0.5)
, xlim=c(-3, 7)
, ylim=c(0,0.35)
, ylab='Proportion'
, add=T)
}
Plot
Problem
The problem is that if I knit this to a pdf, I get the error could not find function "alpha"
. How can I get the transparency of the plots for the pdf export? A base R solution would be ideal.