I am creating 2 overlapping histograms on a single plot. The diagram needs to be clear in black and white, so I am using a combination of colours and textures to help distinguish between the histograms.
The problem is with the legend, where I can either get the colours to show up, or the textures, but not both. This code produces a legend with empty boxes:
dat <- rnorm(100)
dat2<-rnorm(100,1)
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", NA), density = c(0, 20))
I don't have the reputation to post images, but a link to the output is below: https://drive.google.com/uc?id=0B4VH8cX4Cf7bNjhVQnUwWWVaMEk
I can get the colour alone to display correctly:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", NA))
https://drive.google.com/uc?id=0B4VH8cX4Cf7bcXJvakZxN0x5c1U
And the textures alone also works:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), density = c(0, 20))
(I've can only post 2 links, also because I don't have enough reputation)
It just won't work together. What am I doing wrong?
Thanks!
Figured it out. The
fill
argument inlegend()
also applies to the shading lines, so when I specified thatfill
for the secondlegend
item wasNA
, it meant the shading lines were invisible.The following code produces the result I want: