简体   繁体   English

scatterPlot出错

[英]error with scatterPlot

I am plotting trajectories (latitude vs longitude) with R's scatterPlot from openair package. 我正在使用来自openair包的R的scatterPlot绘制轨迹(纬度与经度)。 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" 此外,我在尝试在后台绘制地图时出错:“使用数据包1时出错。长度为零的参数i”

openair错误scatterPlot

Thanks Ilik 谢谢伊利克

Appears to be a bug in the code (which is based on lattice::xyplot). 似乎是代码中的错误(基于lattice :: xyplot)。 The code of openair::scatterPlot looks kind of amateurish: 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

Using the dodge of renaming the grouping variable and then using $ with mydata$MyGroupVar is a hack. 使用重命名分组变量的躲闪然后使用$与mydata$MyGroupVar是一个黑客攻击。 Should have been done much more simply and less error-prone with mydata[[group]] . 应该用mydata[[group]]做得更简单,更不容易出错。 Suggest you ask for a bug-fix. 建议你要求修复bug。

If you do a traceback() you can see the parameters being passed when the packet error was generated: 如果执行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, 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),默认= 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. 它应该作为一个未引用的,真正的R名称/符号传递。

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)

在此输入图像描述

If you need to tweak this, then see ?xyplot . 如果您需要调整它,请参阅?xyplot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM