Reputation: 3
I want to plot a Polygon in a map. For this I use ggmap at the moment, but it doesnt let me do it giving me always an error message:
Fehler: ggplot2 none-numeric argument for binary Operator
My code looks like this:
> library(rgdal)
> library(geojsonio)
> library(sp)
> library(maps)
> library(ggmap)
> library(maptools)
> data_file <- "/home/jan/Downloads/map.geojson"
> data_json <- geojson_read(data_file, what = "sp")
> plot(data_json, usePolypath = FALSE)
> mapImage <- ggmap(get_googlemap(c(lon = -118.243683, lat = 34.052235), scale = 1,
+ zoom = 7), extent = "normal")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=34.052235,-118.243683&zoom=7&size=640x640&scale=1&maptype=terrain&sensor=false
> dat <- as.data.frame(data_json)
> names(data)[1:2] <- c("lon","lat")
> print(mapImage)+
+ geom_polygon(data = dat, aes(lon, lat), colour="red", fill="red")
Fehler in print(mapImage) + geom_polygon(data = dat, aes(lon, lat), colour = "red", :
nicht-numerisches Argument für binären Operator
but if I do
> dat2 <- as.numeric(dat)
> print(mapImage)+
+ geom_polygon(data = dat2, aes(lon, lat), colour="red", fill="red")
Fehler: ggplot2 doesn't know how to deal with data of class numeric
I get the error
ggplot2 doesn't know how to deal with data of class numeric
PS. Iḿ very new to R and programming
Thank you
Upvotes: 0
Views: 1025
Reputation: 94182
This:
data_json <- geojson_read(data_file, what = "sp")
returns an sp
class object, which ggplot
and geom_polygon
can't deal with.
The fix is to run fortify
on it to make a data frame that geom_polygon
can use. You've not supplied us with your data file so we can't give you the exact code, but:
data_file <- system.file("examples", "california.geojson", package = "geojsonio")
data_json <- geojson_read(data_file, what = "sp")
fd = fortify(data_json)
mapImage + geom_polygon(data=fd, aes(x=long,y=lat))
should give you enough clues.
Upvotes: 1