散点图错误

error with scatterPlot

我正在使用来自 openair 包的 R 散点图绘制轨迹(纬度与经度)。使用 'group' 选项对轨迹进行分组时,不会绘制最后一组。 这是一个示例代码:

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")

此外,我在尝试在后台绘制地图时遇到错误:"Error using packet 1. argument i of length zero"

谢谢 喜欢

似乎是代码中的错误(基于 lattice::xyplot)。 openair::scatterPlot 的代码看起来有点业余:

# ------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

使用重命名分组变量的方法,然后将 $ 与 mydata$MyGroupVar 一起使用是一种 hack。 mydata[[group]] 应该做得更简单,更不容易出错。建议您要求修复错误。

如果您执行 traceback() 您可以看到在生成数据包错误时传递的参数:

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,

(我认为)..."name" 周围的引号可能导致错误。它应该作为未引用的真实 R name/symbol.

传递

我认为你可以用普通格子得到非常接近你想要的东西:

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

如果您需要对此进行调整,请参阅 ?xyplot