2016-03-03 64 views
1

我正在繪製軌跡(緯度與經度)與R的scatterPlot從openair包。當使用「組」選項對軌跡進行分組時,最後一組不會被繪製。 下面是一個示例代碼:錯誤與散點圖

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錯誤」

openair error scatterPlot

謝謝 Ilik

回答

3

似乎是代碼中的錯誤(基於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 =緯度〜經度|默認情況下,數據=列表(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,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 (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)),groups =「name」,type = c(「p」 ),as.table = TRUE,

(我認爲)... The 「名稱」周圍的引號可能會導致錯誤。它應該作爲一個不帶引號的真正的R名稱/符號傳遞。

我認爲你可以得到相當接近你想要用普通格子什麼:

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

enter image description here

如果您需要調整這個,再看看?xyplot

+0

謝謝。對於繪圖(使用地圖),我使用了以下代碼: map(「world」,ylim = c(0,100),xlim = c(100,200),fill = TRUE,bg =「gray」,col =「白色「,mar = c(1,1,1,1)) with(df,points(lon,lat,col = name,pch ='。',cex = 4)) – Ilik