I would like to outline the map of Spain in R and add some points to it at certain coordinates that I have in a CSV file;
how are you:
Aranjuez<-39.595520, -3.391830
San Martin de la vega<- 40.225155-3.530613
Ruta de las vegas<- 40.320294-3.205903
Mondejar<- 40.345348-3.137145
How could I do it?
I have tried with these scripts from another forum and I can't do it.
When I run the map.unizar code I get this error:
Error in if (lon < -180 || lon > 180) { :
missing value where TRUE/FALSE needed
Script: I locate my alma mater
unizar <- geocode('Universidad de Zaragoza, Zaragoza, España')
i get a map
map.unizar <- get_map( location = as.numeric(unizar),
color = "color",
maptype = "roadmap",
scale = 2,
zoom = 16)
I represent it
ggmap(map.unizar)
I add points
ggmap(map.unizar) + geom_point(aes(x = lon, y = lat),
data = unizar, colour = 'red',
size = 4)
Thanks in advance Kind regards
The problem you have is actually that you can't get the latitude and longitude, let's see:
This in console throws the following message
If we go directly to the link ( http://maps.googleapis.com/maps/api/geocode/json?address=Universidad%20de%20Zaragoza,%20Zaragoza,%20Espa%F1a&sensor=false ) through a browser we will get an error more descriptive:
In other words, the address we want to search for is not well coded, certain characters (surely the ñ) do not have an
utf-8
adequate code. To solve it we can useenc2utf8()
:and now yes, everything that follows:
I added a control to know if we have been able to correctly retrieve latitude and longitude, Google can "ban" you at some point if you make many queries and it would have to be controlled.
The result:![enter image description here](https://i.stack.imgur.com/qqT4T.png)
If what you want is the complete map of Spain you can modify the zoom level, also remember that if you already have the latitude and longitude, you do not need to call
geocode()
:The result:
If you are still not convinced by the result, I suggest you take a look at LeafLet , which offers you a dynamic, very configurable and attractive visualization:
Example exported to an image: