[R] lattice to ggplot2 conversion help

Ista Zahn izahn at psych.rochester.edu
Mon Aug 22 17:07:52 CEST 2011


Hi Ashz,

On Mon, Aug 22, 2011 at 8:42 AM, ashz <ashz at walla.co.il> wrote:
> Hi,
>
> I am interested in ggplot2 and I found this lattice code very interesting
> (http://addictedtor.free.fr/graphiques/graphcode.php?graph=48).
>
> Code:
> library(lattice)
> lattice.options(default.theme = canonical.theme(color = FALSE))
>
>
> tmp <-
>    expand.grid(geology = c("Sand","Clay","Silt","Rock"),
>                species = c("ArisDiff", "BracSera", "CynDact",
>                            "ElioMuti", "EragCurS", "EragPseu"),
>                dist = seq(1,9,1) )
>
> tmp$height <- rnorm(216)
>
>
> sp <- list(superpose.symbol = list(pch = 1:6, cex = 1.2),
>           superpose.line = list(col = "grey", lty = 1))
>
> # print is needed when you source() the file
> print(xyplot(height ~ dist | geology, data = tmp,
>       groups = species,
>       layout = c(2,2),
>       panel = function(x, y, type, ...) {
>         panel.superpose(x, y, type="l", ...)
>         lpoints(x, y, pch=16, col="white", cex=2)
>         panel.superpose(x, y, type="p",...)
>       },
>       par.settings = sp,
>       auto.key = list(columns = 2, lines = TRUE)))
>
>
>
> I will be very happy if someone can please explain me how to do it in
> ggplot2 as it will be great help.

The basic plot can be created with

ggplot(tmp, aes(x = dist, y = height)) +
  geom_point(aes(shape = species)) +
  geom_line(aes(group = species)) +
  facet_wrap(~geology)

and some of the formatting can be reproduced with

ggplot(tmp, aes(x = dist, y = height)) +
  geom_point(aes(shape = species), size = 4) +
  geom_line(aes(group = species), color = "gray60") +
  facet_wrap(~geology) +
  theme_bw() +
  opts(legend.position = "top", legend.direction = "horizontal")

I do have to say that I think this graph is  a mess though. Too many
jumbled points and lines to easily make sense of it. I would go with
small multiples all the way:

ggplot(tmp, aes(x = dist, y = height)) +
  geom_point() +
  geom_line() +
  facet_grid(species~geology) +
  theme_bw() +
  opts(legend.position = "top", legend.direction = "horizontal")

as this seems like a much clearer presentation of the data.

Best,
Ista
>
> Cheers,
> Ashz
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/lattice-to-ggplot2-conversion-help-tp3760001p3760001.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list