Search code examples
rscatter-plotopenair

error with scatterPlot


I am plotting trajectories (latitude vs longitude) with R's scatterPlot from openair package. When grouping the trajectories with the 'group' option, the last group doesn't get plotted. Here is a sample code:

df = data.frame(name = c(rep('C1',10),rep('C2',10),rep('C3',10),rep('C4',10)),
            lat = seq(1,100,2.5),
            lon = seq(101,200,2.5))
scatterPlot(df    ,x = "lon", y = "lat", group = "name",map = TRUE )
scatterPlot(df    ,x = "lon", y = "lat")

Also, I get an error when trying to plot a map in the background: "Error using packet 1. argument i of length zero"

openair error scatterPlot

Thanks Ilik


Solution

  • Appears to be a bug in the code (which is based on lattice::xyplot). The code of openair::scatterPlot looks kind of amateurish:

    # ------segment where `group` parameter is passed to lattice code ---
        id <- which(names(mydata) == group)
        names(mydata)[id] <- "MyGroupVar"
        plotType <- if (!Args$traj) 
            c("p", "g")
        else "n"
        if (method == "scatter") {
            if (missing(k)) 
                k <- NULL
            Type <- type
            xy.args <- list(x = myform, data = mydata, groups = mydata$MyGroupVar, 
                type = plotType, as.table = TRUE, scales = scales, 
        #---- end of extract
    

    Using the dodge of renaming the grouping variable and then using $ with mydata$MyGroupVar is a hack. Should have been done much more simply and less error-prone with mydata[[group]]. Suggest you ask for a bug-fix.

    If you do a traceback() you can see the parameters being passed when the packet error was generated:

    traceback() 4: xyplot.formula(x = lat ~ lon | default, data = list(lon = c(101, 103.5, 106, 108.5, 111, 113.5, 116, 118.5, 121, 123.5, 126, 128.5, 131, 133.5, 136, 138.5, 141, 143.5, 146, 148.5, 151, 153.5, 156, 158.5, 161, 163.5, 166, 168.5, 171, 173.5, 176, 178.5, 181, 183.5, 186, 188.5, 191, 193.5, 196, 198.5), lat = c(1, 3.5, 6, 8.5, 11, 13.5, 16, 18.5, 21, 23.5, 26, 28.5, 31, 33.5, 36, 38.5, 41, 43.5, 46, 48.5, 51, 53.5, 56, 58.5, 61, 63.5, 66, 68.5, 71, 73.5, 76, 78.5, 81, 83.5, 86, 88.5, 91, 93.5, 96, 98.5), default = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), MyGroupVar = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), groups = "name", type = c("p", "g"), as.table = TRUE,

    (I think) ...The quotes around "name" may be causing the error. It should be getting passed as an unquoted, true R name/symbol.

    I think you can get pretty close to what you want with ordinary lattice:

    xyplot(data=df  ,  lon~lat, groups = name, auto.key=TRUE, grid=TRUE)
    

    enter image description here

    If you need to tweak this, then see ?xyplot.