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

John Kane jrkrideau at inbox.com
Thu Jun 7 17:12:53 CEST 2012


   Well, a brute force and stupidity approach with geom_text will work but it's
   not aesthetically very nice.  Note I did not play around with text size.
   Try :
   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") +
           geom_text(data=NULL, x =11 , y= 0.75, colour= "orange", label =
   "London") +
            geom_text(data=NULL, x =16 , y= 0.75, colour= "blue", label =
   "NYC")
   I  thought that one should be able to generate a second legend with an
   aes(colour=ids) but it does not work.
   Someone much more knowledgeable than me hopefully will have a better idea.

   John Kane
   Kingston ON Canada

   -----Original Message-----
   From: tim_smith_666 at yahoo.com
   Sent: Thu, 7 Jun 2012 07:30:08 -0700 (PDT)
   To: jrkrideau at inbox.com, bsmith030465 at gmail.com, r-help at r-project.org
   Subject: Re: [R] ggplot2: legend for geom_rug() ..?

   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")
   Alternatively, how do I add a second legend in ggplot2?
   thanks!
     _________________________________________________________________

   From: John Kane <jrkrideau at inbox.com>
   To: Brian Smith <bsmith030465 at gmail.com>; r-help at r-project.org
   Sent: Wednesday, June 6, 2012 3:06 PM
   Subject: Re: [R] ggplot2: legend for geom_rug() ..?
   What is X2?
   code not running at the moment
   John Kane
   Kingston ON Canada
   > -----Original Message-----
   > From: [1]bsmith030465 at gmail.com
   > Sent: Wed, 6 Jun 2012 11:52:25 -0400
   > To: [2]r-help at r-project.org
   > Subject: [R] ggplot2: legend for geom_rug() ..?
   >
   > Hi,
   >
   > I was trying to make another legend for the rug plot. Sample code:
   >
   >
   > library(ggplo2)
   > 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)
   >
   > ggplot(mdat, aes(position, value)) + geom_point(aes(colour = X2)) +
   >        geom_rug(subset = .(position < 14),aes(y=NULL),color="orange") +
   >        geom_rug(subset = .(position > 14),aes(y=NULL),color="black")
   >
   >
   >
   >
   > This gives the plot correctly, but how can I add another legend that
   > would
   > give some more information on the rugplot (e.g. that 'orange' line =
   > 'London', and 'black' line = 'NYC')?
   >
   > thanks!!
   >
   >     [[alternative HTML version deleted]]
   >
   > ______________________________________________
   > [3]R-help at r-project.org mailing list
   > [4]https://stat.ethz.ch/mailman/listinfo/r-help
   > PLEASE do read the posting guide
   > http://www.R-project.org/posting-guide.html
   > and provide commented, minimal, self-contained, reproducible code.
   ____________________________________________________________
   GET   FREE   SMILEYS   FOR   YOUR   IM   &   EMAIL  -  Learn  more  at
   http://www.inbox.com/smileys
   Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and
   most webmails
   ______________________________________________
   [5]R-help at r-project.org mailing list
   [6]https://stat.ethz.ch/mailman/listinfo/r-help
   PLEASE do read the posting guide
   [7]http://www.R-project.org/posting-guide.html
   and provide commented, minimal, self-contained, reproducible code.
     _________________________________________________________________

   [8]Email Notifier Preview 
   Receive Notifications of Incoming Messages
   Easily monitor multiple email accounts & access them with a click. Visit
   [9]www.inbox.com/notifier and check it out!

References

   1. mailto:bsmith030465 at gmail.com
   2. mailto:r-help at r-project.org
   3. mailto:R-help at r-project.org
   4. https://stat.ethz.ch/mailman/listinfo/r-help
   5. mailto:R-help at r-project.org
   6. https://stat.ethz.ch/mailman/listinfo/r-help
   7. http://www.R-project.org/posting-guide.html
   8. http://www.inbox.com/notifier
   9. http://www.inbox.com/notifier


More information about the R-help mailing list