[R] ggplot2: legend for geom_rug() ..?

Hadley Wickham hadley at rice.edu
Wed Aug 15 21:37:31 CEST 2012


On Thu, Jun 7, 2012 at 9:30 AM, Tim Smith <tim_smith_666 at yahoo.com> wrote:
> Hi,
>
> Here is the corrected code:
>
> library(ggplot2)
> ids <- paste('id_',1:3,sep='')
> before <- sample(9)
> after <- sample(1:10,9)
> dat <- as.matrix(cbind(before,after))
> rownames(dat) <- rep(ids,3)
> position <- c(rep(10,3),rep(13,3),rep(19,3))
>
> mdat <- cbind(melt(dat),position)
> colnames(mdat) <- c('ID','Treatment','value','position')
>
> ggplot(mdat, aes(position, value)) + geom_point(aes(colour = Treatment)) +
>         geom_rug(subset = .(position < 14),aes(y=NULL),color="orange") +
>         geom_rug(subset = .(position > 14),aes(y=NULL),color="black")

Here's one way:

ggplot(mdat, aes(position, value)) + geom_point(aes(colour = Treatment)) +
        geom_rug(subset = .(position < 14),aes(y=NULL, color="<14") +
        geom_rug(subset = .(position > 14),aes(y=NULL, color=">14") +
        scale_colour_manual(values = c("<14" = "orange", ">14" = "black")

Hadley



-- 
Assistant Professor
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list