Adding key legend to multi-histogram plot in R

histogram, key, legend, r

Solution

The legend command will do the trick:

legend("topright", c("Germany", "Plastic"), col=c("blue", "red"), lwd=10)

To get the two short horizontal colour bars just use the a standard line, but increase the line thickness. As Roland pointed out, you can also use the `fill` argument:

legend("topright", c("Germany", "Plastic"), fill=c("blue", "red"))

See `?legend` for more details.

Problem

How do I add a key legend to the below plot I whish to have a key legend somewhere in the upper right corner with two short horizontal color bars, where the red one should say "Plastic surgery gone wrong" and the blue one should say "Germany". I used the following code to produce the plot: ``` bar2 <- read.table("div/ana-mut[...]/barriers-set-2.dat", sep=" ") bar2val <- c(bar2$V1, bar2$V2) bar3 <- read.table("div/ana-mut[...]/barriers-set-3.dat", sep=" ") bar3val <- c(bar3$V1, bar3$V2) p1 <- hist(subset(bar2val, bar2val < 30), breaks=30) p2 <- hist(subset(bar3val, bar3val < 30), breaks=30) plot(p1, col=rgb(1,0,0,8/9), main="Barrier distribution", xlab="Barrier [kcal/mol]", ylab="Mutant count") plot(p2, col=rgb(0,0,1,8/9), add=T) ``` Any hints would be greatly appreciated.

Original source

Related problems