Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a legend to a ggplot - invalid color name

Tags:

r

ggplot2

I have looked around in the documentation and posts like this, but it is not clear to me. Apparantly a legend is created automatically and I have to map colors to explaining text.

An example with data:

vtm1<- c(1,3,4)
vmean<-c(3.9,3.8,3)
vmax<-c(4.1,4.2,4)
vmin<-c(3,2.5,2)
rtm1<- c(1,2,4,5)
rmean<-c(3.9,3.85,3.7,3.1)
rmax<-c(4.1,4.2,4,3.9)
rmin<-c(3,2.5,2,1.9)
gtm1<- c(2,4,5)
gmean<-c(4.1,3.9,3)
gmax<-c(4.1,4,3.9)
gmin<-c(3,2.5,1.5)
vns <- data.frame(vtm1, vmean, vmax, vmin)
gibbs <- data.frame(gtm1, gmean, gmax, gmin)
rw <- data.frame(rtm1, rmean, rmax, rmin)

plt <- ggplot(NULL, aes(x= vtm1))
plt + xlab("Number of TM lookups") + ylab("Cross-entropy") + 
xlim(0, 30000000) + 
ylim(3.15,4.2)+  
geom_ribbon(data=gibbs, aes(x = gtm1, ymin= gmin, ymax= gmax), fill="#A8A8A8") +  
geom_ribbon(data=rw, alpha=0.5, aes(x = rtm1, ymin= rmin, ymax= rmax), fill="red")+  
geom_ribbon(data=vns, alpha=0.5, aes(x = vtm1, ymin= vmin, ymax= vmax), fill="mycolor")+ 
geom_line(data=gibbs, linetype = "dotdash", aes(x=gtm1,y=gmean)) + 
geom_line(data=vns,aes(x=vtm1,y=vmean)) + geom_line(data=rw, linetype = "dashed", aes(x=rtm1,y=rmean)) + 
geom_hline(yintercept=3.2240952381, linetype = "dotted", color="mycolor2")+ 
geom_hline(yintercept=3.44366666666667, linetype = "dotted") + 
theme_bw( )+
    scale_colour_manual(name="Lines", values=c("mycolor"="blue","mycolor2"="red")) +  
    scale_linetype_manual(name="Lines", values=c("mycolor"="dotted","mycolor2"="dashed"))

R keeps giving me the error:

Error in col2rgb(colour, TRUE) : invalid color name 'mycolor'
like image 784
dorien Avatar asked Mar 18 '14 15:03

dorien


2 Answers

Here is a simplified example that should illustrate the general philosophy of ggplot2:

names(vns) <- c("tm1", "mean", "max", "min")
names(gibbs) <- c("tm1", "mean", "max", "min")
names(rw) <- c("tm1", "mean", "max", "min")
vns$what <- "vns"
gibbs$what <- "gibbs"
rw$what <- "rw"

DF <- do.call(rbind, list(vns, gibbs, rw))

plt <- ggplot(DF, aes(x= tm1, ymin= min, ymax= max, y=mean)) +
  geom_ribbon(aes(fill=what), alpha=0.3) +  
  geom_line(aes(colour=what))
print(plt)

enter image description here

As you see, all the data lives in one data.frame and a factor variable is used to group it and mapped to fill/colour/ ... You can than use scale_manual_* if you are not happy with defaults.

like image 90
Roland Avatar answered Oct 10 '22 08:10

Roland


I can't get this to work properly with geom_hline, but if you're okay using geom_line then you can use:

library(ggplot2)
ggplot(NULL, aes(x= 1:30 * 1e6)) +  # made up some `x` data
  geom_point(aes(y=seq(3.5, 4.2, len=30), color="mycolor")) +  # adding geom_point for show
  geom_line(aes(x=c(0, 3e7), y=3.22409, color="mycolor", linetype="mycolor")) +
  scale_colour_manual(name="Lines", values=c("mycolor"="blue")) +  
  scale_linetype_manual(name="Lines", values=c("mycolor"=3)) +
  xlab("Number of TM lookups") + 
  ylab("Cross-entropy") + xlim(0, 30000000) + ylim(3.15,4.2)

enter image description here

In addition to changing to geom_line, you also need to make sure your linetype mappings are numeric (i.e. we use 3 for dotted here).

like image 37
BrodieG Avatar answered Oct 10 '22 09:10

BrodieG