【问题标题】:error with scatterPlot散点图错误
【发布时间】:2018-02-22 16:42:29
【问题描述】:

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

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

另外,当我尝试在后台绘制地图时出现错误:“使用数据包 1 时出错。长度为零的参数 i”

谢谢 伊力克

【问题讨论】:

    标签: r scatter-plot openair


    【解决方案1】:

    似乎是代码中的一个错误(基于 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 一起使用是一种技巧。使用mydata[[group]] 应该更简单,更不容易出错。建议您要求修复错误。

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

    回溯() 4: xyplot.formula(x = lat ~ lon | 默认, 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), 纬度 = 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), 默认 = 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,

    (我认为)......“名称”周围的引号可能导致错误。它应该作为未加引号的真实 R 名称/符号传递。

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

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

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

    【讨论】:

    • 谢谢。对于绘图(带有地图),我使用了以下代码: map("world", ylim=c(0, 100), xlim=c(100,200), fill=TRUE, bg="gray", col="white ",mar = c(1,1,1,1)) with(df, points(lon, lat, col=name,pch='.',cex=4))
    猜你喜欢
    • 2017-07-25
    • 2018-05-04
    • 2019-07-17
    • 2016-06-14
    • 2021-04-15
    • 1970-01-01
    • 2019-01-23
    相关资源
    最近更新 更多