Error in plot.sfc_POLYGON(x, y, main = "Online and instore customer loc", : missing(y) is not TRUE

hello code is giving error i didnot get the point why its not runing plot. please help

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
set.seed(100)
X=7.5
Y=7.5
f = function(){
  a = runif(10,1,10)
  
  return( a=as.integer(a))
  
}
onlineloc=data.frame(x=f(),y=f())
instore=data.frame(a=f(),b=f())
store=data.frame(X,Y)
plot(x,y,main="Online and instore customer loc",col=" red")
#> Error in plot(x, y, main = "Online and instore customer loc", col = " red"): object 'x' not found
points(a,b,pch=2)
#> Error in points(a, b, pch = 2): object 'a' not found
points(X,Y,pch=15)
#> Error in plot.xy(xy.coords(x, y), type = type, ...): plot.new has not been called yet
legend(x=3.5,y=5,legend=c("Instore Customer","Online Customer","Store"),pch=c(2,1,15),col=c("black","red","black"))
#> Error in strwidth(legend, units = "user", cex = cex, font = text.font): plot.new has not been called yet
#plot(onlineloc,main="Online customer loc",col=" red")

Created on 2019-10-20 by the reprex package (v0.3.0)

Hi @Emmadadil. You cannot directly pass the column names x, y, a, b, X, Y of data frame to plot. You have to pass the whole data.frame into it.

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.3.2, PROJ 6.2.0

set.seed(100)
X=7.5
Y=7.5
f = function(){
  a = runif(10,1,10)
  
  return( a=as.integer(a))
  
}
onlineloc=data.frame(x=f(),y=f())
instore=data.frame(a=f(),b=f())
store=data.frame(X,Y)
plot(onlineloc,main="Online and instore customer loc",col=" red")

points(instore,pch=2)

points(store,pch=15)

legend(x=3.5,y=5,legend=c("Instore Customer","Online Customer","Store"),pch=c(2,1,15),col=c("black","red","black"))

Created on 2019-10-20 by the reprex package (v0.3.0)

Or pass the column values of data frame into it.

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.3.2, PROJ 6.2.0

set.seed(100)
X=7.5
Y=7.5
f = function(){
  a = runif(10,1,10)
  
  return( a=as.integer(a))
  
}
onlineloc=data.frame(x=f(),y=f())
instore=data.frame(a=f(),b=f())
store=data.frame(X,Y)
plot(onlineloc$x, onlineloc$y,main="Online and instore customer loc",col=" red")

points(instore$a, instore$b,pch=2)

points(store$X, store$Y,pch=15)

legend(x=3.5,y=5,legend=c("Instore Customer","Online Customer","Store"),pch=c(2,1,15),col=c("black","red","black"))

Created on 2019-10-20 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.