Multiple rugs

546 views
Skip to first unread message

Stavros Macrakis

unread,
May 7, 2009, 4:28:27 PM5/7/09
to ggplot2
How do I go about creating multiple (non-overlapping) rugs?  + geom_rug(...) + geom_rug(...) puts them both in the same space.  I understand that rug lines can have multiple colors, but sometimes the density of the data is such that multiple rugplots would be helpful....

            -s

baptiste auguie

unread,
May 8, 2009, 6:13:36 AM5/8/09
to macr...@alum.mit.edu, ggplot2
Hi,

It seems hard-coded, but you could slightly modify the geom and create your own like this,


GeomRug <- proto(Geom, {
  draw <- function(., data, scales, coordinates, ...) {
    rugs <- list()
    data <- coordinates$transform(data, scales)
    if (!is.null(data$x)) {
      rugs$x <- with(data, segmentsGrob(
        x0 = unit(x, "native"), x1 = unit(x, "native"),
        y0 = unit(ystart, "npc"), y1 = unit(ystart, "npc") + unit(ylen, "npc"),  # FIRST MODIF
        gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = size * .pt)
      ))
    }

    if (!is.null(data$y)) {
      rugs$y <- with(data, segmentsGrob(
        y0 = unit(y, "native"), y1 = unit(y, "native"),
        x0 = unit(xstart, "npc"), x1 = unit(xstart, "npc") + unit(xlen, "npc"),  # SECOND MODIF
        gp = gpar(col = alpha(colour, alpha), lty = linetype, lwd = size * .pt)
      ))
    }

    gTree(children = do.call("gList", rugs))
  }

  objname <- "rug"

  desc <- "Free rug plots"

  default_stat <- function(.) StatIdentity
 
  default_aes <- function(.) # THIRD MODIF
        aes(colour="black", size=0.5, linetype=1, alpha = 1, ystart=0, ylen=0.03, xstart=0, xlen=0.03) 
  guide_geom <- function(.) "path"

  examples <- function(.) {
    p <- ggplot(mtcars, aes(x=wt, y=mpg))
    p + geom_point()
    p + geom_point() + geom_rug()
    p + geom_point() + geom_rug(position='jitter')
  }


})


p <- ggplot(mtcars, aes(x=wt, y=mpg))

p + geom_point() + geom_rug()
p + geom_point() + geom_rug(xstart=0.5)

I'm not sure how to rename it as a new geom though, and it would need some thoughts about stacking several rugs on top of each other automatically (whether that's a good idea or not I dunno).

HTH,

baptiste
> Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________





From: Stavros Macrakis <macr...@gmail.com>
To: ggplot2 <ggp...@googlegroups.com>
Sent: Thursday, 7 May, 2009 22:28:27
Subject: Multiple rugs

How do I go about creating multiple (non-overlapping) rugs?  + geom_rug(...) + geom_rug(...) puts them both in the same space.  I understand that rug lines can have multiple colors, but sometimes the density of the data is such that multiple rugplots would be helpful....

            -s

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the ggplot2 mailing list.
To post to this group, send email to ggp...@googlegroups.com
To unsubscribe from this group, send email to
ggplot2+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ggplot2
-~----------~----~----~----~------~----~------~--~---


hadley wickham

unread,
May 8, 2009, 9:02:09 AM5/8/09
to macr...@alum.mit.edu, ggplot2
I've never found much use for rug plots. But if Baptiste's suggestion
doesn't work, you can always draw your own with geom_segment.

Hadley
--
http://had.co.nz/

Stavros Macrakis

unread,
May 8, 2009, 1:02:15 PM5/8/09
to ggplot2
I'm not particularly fond of rug plots, either.  But until someone
(maybe me if I can figure out how...) adds density plots to the axes,
they're a quick and dirty way of seeing density along each axis.  When
they get too dense, I sometimes will sample from the original
distribution to generate a more legible rugplot....

               -s

Peter Flom

unread,
May 8, 2009, 1:26:28 PM5/8/09
to macr...@alum.mit.edu, ggplot2
Stavros Macrakis <macr...@gmail.com> wrote

>I'm not particularly fond of rug plots, either.  But until someone
>(maybe me if I can figure out how...) adds density plots to the axes,
>they're a quick and dirty way of seeing density along each axis.  When
>they get too dense, I sometimes will sample from the original
>distribution to generate a more legible rugplot....
>

I've lost the origin of this thread, but for adding box plots to each axis
of a scatterplot there is the scatterplot function in the car package, which does other
nice things as well.

Peter

Peter L. Flom, PhD
Statistical Consultant
www DOT peterflomconsulting DOT com

Mike Lawrence

unread,
May 17, 2009, 4:30:47 PM5/17/09
to macr...@alum.mit.edu, ggplot2
Here's my attempt at adding density plots to each axis. Unfortunately,
the density plots don't properly line up with the scatterplot and
there's a lot of white space between the scatter and density plots.

d = data.frame(V1 = rnorm(100),V2 = rnorm(100))
p1 = ggplot()+
layer(
data = d
, mapping = aes(
x = V1
, y = V2
)
, geom = 'point'
)+
scale_x_continuous(
limits = c(min(d$V1),max(d$V1))
)


p2 = ggplot()+
layer(
data = d
, mapping = aes(
x = V1
, y = ..density..
)
, geom = 'line'
, stat = 'density'
)+
scale_x_continuous(
limits = c(min(d$V1),max(d$V1))
)+
opts(
panel.grid.minor = theme_blank()
,panel.grid.major = theme_blank()
,axis.ticks = theme_blank()
,axis.text.y = theme_blank()
,axis.text.x = theme_blank()
,axis.title.y = theme_blank()
,axis.title.x = theme_blank()
)

p3 = ggplot()+
layer(
data = d
, mapping = aes(
x = V2
, y = ..density..
)
, geom = 'line'
, stat = 'density'
)+
scale_x_continuous(
limits = c(min(d$V2),max(d$V2))
)+
opts(
panel.grid.minor = theme_blank()
,panel.grid.major = theme_blank()
,axis.ticks = theme_blank()
,axis.text.y = theme_blank()
,axis.text.x = theme_blank()
,axis.title.y = theme_blank()
,axis.title.x = theme_blank()
)+
coord_flip()

size = .7
print(p1,vp = viewport(width = size , height = size , x = size*.5 , y
= size*.5 ))
print(p2,vp = viewport(width = size , height = 1-size , x = size*.5 ,
y = 1-(1-size)*.5 ))
print(p3,vp = viewport(width = 1-size , height = size , x =
1-(1-size)*.5 , y = size*.5 ))


--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tr.im/mikes_public_calendar

~ Certainty is folly... I think. ~

Reply all
Reply to author
Forward
0 new messages