[R] lattice : superpose symbols with a great many points

Laurent Rhelp LaurentRHelp at free.fr
Mon Jun 23 18:35:38 CEST 2014


Le 23/06/2014 01:03, Dennis Murphy a écrit :
> Hi:
>
> There are times when lattice is easier than ggplot2 and times when the
> converse is true. This is a case where ggplot2 is easier to code once
> you understand what it is doing. I'm going to try to reproduce your
> lattice graph in ggplot2 as follows:
>
> library(ggplot2)
>
> # Set up the rows of data.sim where points should be plotted
> pidx <- seq(1, nrow(data.sim), by = 100)
>
> # Default appearance, plotting lines and points; the shapes are
> # specified in scale_shape_manual()
> p <- ggplot(data.sim, aes(x = time, y = value, colour = essai, shape = essai)) +
>      geom_vline(xintercept = 0, linetype = "dotted") +
>      geom_line() +
>      geom_point(data = data.sim[pidx, ]) +
>      scale_shape_manual(values = 0:4)
> p      # render it in the graphics window
>
> # Change some background elements to more closely resemble the lattice
> # plot:
> #  * change the colors of ticks and tick labels
> #  * remove grid lines
> #  * put a bounding box on the graphics panel
>
> p + theme_bw() +
>     theme(panel.grid.major = element_blank(),
>           panel.grid.minor = element_blank(),
>           axis.text = element_text(color = "black"),
>           axis.ticks = element_line(color = "black"),
>           panel.border = element_rect(color = "black"))
>
>
>
> After doing this, I realized your lattice code could be simplified
> with the latticeExtra package:
>
> library(latticeExtra)
>
> # Set up the rows of data.sim where points should be plotted
> pidx <- seq(1, nrow(data.sim), by = 100)
>
> # Plot the lines
> p1 <- xyplot(value ~ time, data = data.sim, groups = essai, type = "l",
>               col = 1:5)
>
> # Plot the points - notice the input data frame
> p2 <- xyplot(value ~ time, data = data.sim[pidx, ], groups = essai,
>               type = "p", pch = 1:5, col = 1:5)
>
> # Plot the vertical line
> p3 <- xyplot(value ~ time, data = data.sim,
>               panel = function(x, y, ...)
>                  panel.abline(v = 0, lty = "dotted", col = "black")
>              )
> p1 + p2 + p3
>
> This is a consequence of the layering principles added to latticeExtra
> a while back, which are designed to emulate the layered grammar of
> graphics (ggplot2). Basically, each plot is a different piece, and you
> can see that each trellis object is similar to the plot layers added
> by geom_*() in ggplot2. This works largely because the scaling is the
> same in all three graphs. One advantage of ggplot2 is that it produces
> pretty decent legends by default. You have to work harder to achieve
> that in lattice, particularly when you divide plots into layers like
> this. (I think you'd need to add the same key = ... code to each plot,
> but I didn't try it.) If the legend is irrelevant to you, then this
> should be fine.
>
> HTH,
> Dennis
>
> On Sun, Jun 22, 2014 at 2:09 PM, Laurent Rhelp <LaurentRHelp at free.fr> wrote:
>> Le 20/06/2014 21:50, Christoph Scherber a écrit :
>>
>>> Dear Laurent
>>>
>>> for numeric x variables, you could try jitter:
>>>
>>> xyplot(y~jitter(x,0.5))
>>>
>>> Cheers
>>> Christoph
>>>
>>>
>>> Am 20.06.2014 21:45, schrieb Laurent Rhelp:
>>>> Hi,
>>>>
>>>>    I like to use with xyplot (package lattice) the groups argument and
>>>> superpose.symbol to compare several curves. But, when there are a great many
>>>> points, the symbols are very close and the graph becomes unreadable. Would
>>>> there be an argument  or a tip not to draw all the symbols, for example the
>>>> symbols could be drawn only every x points, x given as an argument ?
>>>>
>>>> Thanks
>>>> Best regards
>>>> Laurent
>>>>
>>>>
>>>>
>>>> ---
>>>> Ce courrier électronique ne contient aucun virus ou logiciel malveillant
>>>> parce que la protection avast! Antivirus est active.
>>>>
>>>> ______________________________________________
>>>> 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.
>>>
>>> ______________________________________________
>>> 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.
>>>
>> It is a very good idea !!! The symbols become readable but it is not an
>> actual answer to the issue.
>>
>> I finally wrote the code below which works but I think there is certainly
>> something easier (shorter) !!!
>>
>> Thanks
>>
>> --o<----------------------------------------------------------------------------------------->o----
>> ##
>> ## 1. mock data
>> ##
>> n <- 10000
>> time <- seq(-10*pi,10*pi,length=n)
>> essai <- c("essai1","essai2","essai3","essai4","essai5")
>>
>> ll <- list()
>> for( i in 1:5){
>>     ll[[i]] <-data.frame(time=time,
>>                          value=sin(time+(pi/i)),
>>                          essai=essai[i])
>> }
>> data.sim <- do.call("rbind",ll)
>> ##
>> ## 2. lattice initialisation for the colors and the symbols
>> ##
>> para.liste <- trellis.par.get()
>> superpose.symbol <- para.liste$superpose.symbol
>> superpose.symbol$pch <- seq(1,5)
>> superpose.symbol$col <- seq(1,5)
>> ##
>> ## 3. lattice code
>> ##
>> xyplot(value ~ time,
>>         data=data.sim,
>>         nr=100,
>>         groups=essai,
>>
>>         panel = function(x,y,subscripts,groups,nr,...) {
>>                         panel.abline(v=0, lty = "dotted", col = "black")
>>                         groupsnames <- levels(groups)
>>                         ng <- length(groupsnames)
>>                         for( i in 1:ng){
>>                              g <- groupsnames[i]
>>                              idg <- g == groups[subscripts]
>>                              superpose.symbol <-
>> trellis.par.get("superpose.symbol")
>>                              ncol <- superpose.symbol$col[i]
>>                              npch <- superpose.symbol$pch[i]
>>
>>                              ## we draw only the line
>> panel.xyplot(x[idg],y[idg],type="l",col=ncol,...)
>>
>>                              ## and then we draw a symbol every nr points
>>                              idx <- seq(1,length(x[idg]),by=nr)
>>                              idy <- seq(1,length(y[idg]),by=nr)
>> panel.points(x[idg][idx],y[idg][idy],col=ncol,pch=npch)
>>                          }
>>              },
>>         par.settings = list(
>>              superpose.symbol=superpose.symbol
>>             )
>>      )
>>
>> --o<--------------------------------------------------------------------------------------->o----
>>
>>
>>
>>
>>
>>
>>
>>
>> ---
>> Ce courrier électronique ne contient aucun virus ou logiciel malveillant
>> parce que la protection avast! Antivirus est active.
>>
>> ______________________________________________
>> 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.
very interesting, thank you for these explanations. I didn't know that 
it was possible to add graphs with lattice and I think I will learn 
ggplot ! I will try to add the key on the lattice graph.






---
Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active.



More information about the R-help mailing list