Adjust font size in map plots in ggmap - r

I am trying to plot a number of points on a map of Germany, using the ggmap package, which works great. However, I would like to make some specific changes and do not know how to or if that is at all possible.
To make the maps, I do the following (d is a data frame with longitudes and latitudes):
map <- get_map(location = c(10.25828, 51.11484), zoom = 6, maptype = "terrain")
p <- ggmap(map)
p <- p + scale_x_continuous(limits = c(6.09500,14.95306)) + scale_y_continuous(limits = c(47.39889,55.01250)+c(0,-0.1))
p <- p + xlab("Longitude") + ylab("Latitude")
On that map, I then plot some points. Now, if I set zoom = 6 as above, then the map is very detailed, and lots of city names are printed, along with some white lines for the roads. This makes the map look a bit too "crowded". Here is what the map looks like:
The size of the pdf is also quite large which is not very practical if I want to have multiple maps.
On the other hand, if I set zoom = 5, then the background is less detailed, less roads etc are shown, and the file is smaller. However, the printed font of the city and country names is too large. Here is what it looks like:
So my question is: Can is somehow get a map with details as in zoom = 5, but with the font size of country and city names as in zoom = 6?
Thanks in advance

If you change from get_map to get_googlemap, you can pass a string to the style argument which will get passed to the Google Static Maps API. See the documentation, and a crucial reference.
I don't think you can change text size, but you can simplify roads and remove certain labels like so:
map <- get_googlemap(center = c(10.25828, 51.11484), zoom = 6, maptype = "terrain",
style = 'feature:road|element:all|visibility:simplified&style=feature:administrative.locality|element:labels|visibility:off')
which returns
If you want more labels, it's probably easier to insert them later in R. Adjust as you like.

As far as i know most map services include the labels in the rendered map tiles. I never had this problem in r, but with other map services.
Rendering with smaller labels using a customizable map service should be possible, but ggmap only offers CloudMade maps which might be a bit too expensive.
I would recommend using a different library if possible, maybe the one described at r-bloggers.com they use a custom made map from MapBox
a free service (up to a certain limit) i would recommend.

Related

overlay flux footprint over a map

I need to overlay a plot over a map (it can be from ggmap or from a file in my pc - not a shapefile).
### Here is the map
library(ggmap)
library(RgoogleMaps)
lat = 49.12978
lon = -122.985
center = c(lat, lon)
bbmap <- get_map(location = c(lon, lat), zoom = 18, maptype = "satellite")
And here! is the data and the code to make the plot that I want to overlay.
library(fields)
image.plot(FFP$x_2d[1,],FFP$y_2d[,1],FFP$fclim_2d)
The main problem is that the plot uses distance to a point to generate a flux density plot, so there are no coordinates (I only have the center coordinates which I used to get the map)
This question is pretty much this! that hasn't been answered, but it seems that the solution is georeferencing the data. However, I don't know how to do that in R. I have tried using the "raster" function to create a 3D raster of the map and the image, using the extent of the map for both, but the plot seems not right because it looks like its rotated.
Ultimately, what I want is to have only the 90% or 80% contour line over the map, not the whole thing.
The final image should look something like this but showing only the most external line, reference image: from this webpage
Hope someone can help!
Thanks!

Google Maps vs. ggplot2/MarMap

Below is a JavaScript page I have created that allows me add and freely move markers on the map. From this map I can figure out the regions I am interested in.
Basically what I want to do is show the same map using ggplot2/MarMap with coastline indicators + bathymetry data. I am really just interested in getting bathymetry data per GPS location, basically getting negative/positive elevation per Lat+Long, so I was thinking if I can plot it then I should be able to export data to a Database. I am also interested in coastline data, so I want to know how close I am (Lat/Long) to coastline, so with plot data I was also going to augment in DB.
Here is the R script that I am using:
library(marmap);
library(ggplot2);
a_lon1 = -79.89836596313478;
a_lon2 = -79.97179329675288;
a_lat1 = 32.76506070891712;
a_lat2 = 32.803624214389615;
dat <- getNOAA.bathy(a_lon1,a_lon2,a_lat1,a_lat2, keep=FALSE);
autoplot(dat, geom=c("r", "c"), colour="white", size=0.1) + scale_fill_etopo();
Here is the output of above R script:
Questions:
Why do both images not match?
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
Is it possible to plot what I am asking for?
I don't know how you got this result. When I use your script, I get an error since the area your are trying to fetch from the ETOPO1 database using getNOAA.bathy() is too small. However, adding resolution=1 (this gives the highest possible resolution for the ETOPO1 database), here is what I get:
To answer your questions:
Why do both images not match?
Probably because getNOAA.bathy() returned an error and the object dat you're using has been created before, using another set of coordinates
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
I have no clue!
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
I urge you to take a look at section 4 of the marmap-DataAnalysis vignette. This section is dedicated to working with big files. You will find there that you can zoom in any area of a bathy object by using (for instance) the subsetBathy() function that will allow you to click on a map to define the desired area
Is it possible to plot what I am asking for? Yes, but it would be much easier to use base graphics and not ggplot2. Once again, you should read the package vignettes.
Finally, regarding the coastline data, you can use the dist2isobath() function to compute the distance between any gps point and any isobath, including the coastline. Guess where you can learn more about this function and how to use it...

Can anyone suggest a good world map visualization for use in Shiny?

Sorry in advance for the wall of text. I am creating a sort of novel type of choropleth map, in which countries are shaded based on different categorical variables. The way I've set up the app, I assign each country an RGB value based on its levels of each of the underlying variables and I want the map to show that RGB value--seems simple, right?
Unfortunately, most of the map visualizations seem to want to do the color selection for me, rather than letting me choose. The best I've been able to do is to treat the data as categorical and I end up with the same number of categories as countries. This worked fairly well for rworldmap. The problem is, I'm developing this for web use, and I'd really like to have tooltips so that you can hover over a particular country and this doesn't work with rworldmap, as it's just a basic plot. Also, the rworldmap output is not particularly nice looking.
Here's the code I used with that:
mapjoin <- joinCountryData2Map(db, joinCode="ISO3",
nameJoinColumn="iso", mapResolution="high")
mapCountryData(mapjoin, nameColumnToPlot="iso", addLegend=FALSE,
catMethod="categorical", colourPalette=db$rgb, mapTitle=input$year)
I have experimented with googleVis, but I was having a lot of trouble with that--the map would just disappear for no reason and I'd have to reload the page, which I believe was an issue with the Shiny bindings in the googleVis package. I ultimately went with googleCharts (https://github.com/jcheng5/googleCharts), which clears up the problems with the bindings.
However, I'm still having problems.
Here's the reactive function:
output$mapviz <- reactive({
db <- genRgb()
list(
data=googleDataTable(db[c("country", "id")]),
options=list(legend="none", projection="kavrayskiy-vii", colors=db$rgb)
)
)}
and here's the output call:
googleGeoChart("mapviz", width="100%", height="780px")
As you can see, there's not a specific way to clue the JS app that it's categorical data, so as a result, it's making a choropleth with 182 different gradient stop points. Usually this works fine, but occasionally something weird happens and a country mysteriously ends up in an intermediate place between colors. I can always tell that there's a problem because certain countries are supposed to be specific colors (for instance, the U.S. will show as #0000FF, and it's pretty obvious when it's not). I've found that I can go to a different chart type (the app uses other googleCharts types) and then return to the map and usually it's fixed. So it's completely inconsistent.
So with that in mind, can anyone suggest a better mapping tool that I can implement in Shiny that would work well for this purpose?
Thanks!
Check out leaflet:
https://rstudio.github.io/leaflet/
It will allow you to:
have pop-ups for each shapefile with more data
explicitly set the colour of shapefiles in R, based on data in your dbf file.
use an open map background
Some example code (not all may be relevant):
map <- leaflet()%>%
addTiles(urlTemplate = url, attribution = HTML(attrib))%>%
addPolygons(data = sub_shape,
fill = TRUE,
fillColor = colors$color, #set color here
fillOpacity = .8,
stroke = TRUE,
weight = 3,
color = "white",
dashArray = c(5,5),
popup = pops
)

R: world maps with ggplot2/ggmap - How to load a png image as a map

I am trying to find a workaround for ggmap's missing support of world maps (i.e. you can't create any maps that show latitudes > 80°, due to idiosyncrasies in the mapproj package).
To a limited extend however, it seems possible to create empty world maps and save them as an image (png etc.), even if you can't use the ggmap object directly as one normally would in ggmap(get_map(...)).
That's why I'd like to load a png (ideally, one I created with ggmap) into ggplot2 and use that as a map instead. How exactly can I do that?
I am aware that you can load background images in ggplot2 (see this stackoverflow question). But I'd also like to plot points on my map - it's important that the latitude/longitude values are mapped correctly.
(Notice: The code in this answer to World map with ggmap provides some code that, in terms of the output, comes close to what I had in mind.)
Here is an example without ggmap that you can use.
require(ggplot2)
require(cshapes)
world <- cshp(date=as.Date("2012-01-1"))
world.points <- fortify(world, region='COWCODE')
world.points2 <- merge(world.points,world#data,by.x="id",by.y="COWCODE",all.x=TRUE )
# Add a variable 'size' per country
world.points2$size <- factor(ifelse(world.points2$AREA < 121600,"small",ifelse(world.points2$AREA > 515000, "large", "medium")))
# Coord_fixed fixes the aspect ratio.
p <- ggplot(world.points2,aes(long,lat,group=group,fill=size)) + geom_polygon(colour="grey50") + coord_fixed()
p

how to plot a part of map but with limitation arround it

how to plot something like this:
see image here, I didn't have 10 reputation so I can not post image, so paste a image URL here.
http://www.flickr.com/photos/97751721#N07/9089566951
or
http://www.flickr.com/photos/97751721#N07/9091786734
right part of this map is a zoom in map with limitation, that is what I want
if it is use R code or other program language will be more great!
If your have everything as Spatial object you could simply plot with standard plot() and set lim to what ever you like.
require(maptools)
shape <- readShapePoints("shapefile.shp")
plot(shape, xlim=c(minXcoordinate, maxXcoordinate) ylim=c(minYcoordinate, maxYcoordinate))
It is a little unclear to me what exactly you want to do. Do you just want to make a map with a specific set of lon/lat boundaries? Do you need to plot data on top of it? Do you need to control the appearance to make it look like the example you give (with line boundaries & minimal geographic information)?
The ggmap package may get you started on this. The syntax goes like this:
require(ggmap)
# The location argument defines the center of the map
exampleMap <- get_map(location = c(lon = -95, lat = 30), zoom=5)
ggmap(exampleMap)
You can then add data to the map if you like (exactly how is left as an exercise to the motivated student!)

Resources