i have saved an image using ggsave function which look as below
but i want to have output like this
al1 <- get_map(location = c(lon = -86.304474, lat = 32.362563), zoom = 11, maptype = 'terrain')
lon<--86.304474
lat<-32.362563
df<-data.frame(lon,lat)
a+ggplot(df)
ggmap(al1)+geom_point(data=df,aes(x=lon,y=lat),size=2)
i tried to removed x and y axis values but problem is that image have white background at the panel but i want only plot image.
In the ggmap() function, you need extent = "device". See ?ggmap::ggmap for more options.
The following will give the result you want.
library(ggmap)
al1 <- get_map(location = c(lon = -86.304474, lat = 32.362563), zoom = 11, maptype = 'terrain')
lon<--86.304474
lat<-32.362563
df<-data.frame(lon,lat)
#a+ggplot(df) # Not sure what you intend here
ggmap(al1, extent = "device")+geom_point(data=df,aes(x=lon,y=lat),size=2)
Related
I am trying to create a simple map with a scale bar using ggmap and the developer version ofggsn. The map seems to load well, but the scale bar measurements seem off. It shouldn't be over 4,000 km from coast to coast in the US. Below is my current code.
##installing packages.. not all are needed
library(tidyverse)
library(ggplot2)
library(sf)
library(ggspatial)
library(devtools)
library(ggmap)
library(maptools)
library(maps)
library(magick)
library(VTrack)
library(cowplot)
library(gridExtra)
## register a google api key
ggmap::register_google(key = "")
##creating the base map
inset <- get_googlemap(center = c(lon = -90, lat = 50), zoom = 3, maptype = "satellite",color = 'bw')
##below just makes the map a little lighter
insetx <- inset
inset_attributes <- attributes(insetx)
inset_transparent <- matrix(adjustcolor(insetx,
alpha.f = 0.9),
nrow = nrow(inset))
attributes(inset_transparent) <- inset_attributes
##installing dev version of ggsn
devtools::install_github('oswaldosantos/ggsn')
##combining the map with the scale bar. Note that scale bar appears to be off.
ggmap(inset_transparent)+
ggsn::scalebar(x.min = -180, x.max = -100,
y.min = 10, y.max = 85,
dist = 2000, transform = TRUE, dist_unit = 'km', model = 'WGS84',
box.fill = c('black', 'black'), st.color = 'white', box.color = 'white')
I am trying to get the black and white version of a stamen map and its giving me the color version. I've tried downloading the map with get_map and get_stamenmap and both give me the color version regardless of whether I specify color as "bw" or "color". Any ideas or work arounds?
library(ggmap)
mapImage <- get_map(location = c(lon = -110.8, lat = 34.7),
source = "stamen",
maptype = "terrain",
color = "bw",
zoom = 7)
g <- ggmap(mapImage)
To get black-and-white stamen maps, use maptype = "toner". The color argument has no effect on stamen maps. You might also want a panel border around the plot. If so, use ggplot's theme_bw() or theme(panel.border = element_rect(fill = NA, colour = "black")).
library(ggmap)
mapImage <- get_map(location = c(lon = -110.8, lat = 34.7),
source = "stamen",
maptype = "toner",
# color = "bw",
zoom = 7)
ggmap(mapImage) + theme_bw()
My solution was get_stamenmap with maptype="toner".
It is like get_map with source="stamen" speaks with a southern soft R and sloppy lisp dialect that stamen does not understand.
library(ggmap)
mapImage <- get_stamenmap(bbox = c(-114,32,-107,37),
source = "stamen",
maptype = "toner",
zoom = 7)
ggmap(mapImage) +theme_bw()
did the trick for me (using Rstudio in Linux, potential bug)
Notice bbox as the alternative to location and theme_bw() as Sandy suggested
I am creating maps using ggmap and am having trouble displaying some polygons and borders in my code. I have a map of a city that has parts of 3 counties in it. I would like to display the city along with the appropriate county lines. If I set the zoom such that all 3 counties are completely visible, then the county lines appear in the map. However, if I zoom to the portion of the city, the county lines disappear.
Example 1: County lines visible on map
tempplot <- get_map(location = c(lon = -97.37605, lat = 32.94748), zoom=9, maptype = 'roadmap')
myplot <- ggmap(tempplot) + borders ("county", colour = "red", alpha = 0.5, region = "Texas")
myplot <- myplot + geom_point(aes(x = -97.37605, y = 32.94748), color = "dodgerblue4", pch = 20, size = 9)
myplot
Image: http://imgur.com/nx3XU2I
Example 2: County lines partially visible on map
tempplot <- get_map(location = c(lon = -97.37605, lat = 32.94748), zoom=10, maptype = 'roadmap')
myplot <- ggmap(tempplot) + borders ("county", colour = "red", alpha = 0.5, region = "Texas")
myplot <- myplot + geom_point(aes(x = -97.37605, y = 32.94748), color = "dodgerblue4", pch = 20, size = 9)
myplot
Example 3: No county lines visible on map
tempplot <- get_map(location = c(lon = -97.37605, lat = 32.94748), zoom=12, maptype = 'roadmap')
myplot <- ggmap(tempplot) + borders ("county", colour = "red", alpha = 0.5, region = "Texas")
myplot <- myplot + geom_point(aes(x = -97.37605, y = 32.94748), color = "dodgerblue4", pch = 20, size = 9)
myplot
Image: http://imgur.com/dIpp6kp
The only difference between these 3 examples is the zoom on the map. I need the map to be at zoom 12 to see the additional details that I will be adding (individual homes), but when I zoom in, the county lines vanish. Any suggestions?
(Sorry about the links to the images ... I am new to the forum and don't have a 10 reputation yet!)
It appears that also the borders you get in example 1 are not 100% correct. You may check with a lower Zoom lever (eg 5).
To me it looks like there is a problem with borders that are cut off by the picture; the function then tries to connect it to an edge that is still visible. In the zoom level you provided even that is not possible, and therefore it has unexpected behaviour.
To sum up: I don't know what exactly the problem is, but maybe this short analysis helps in any way!
I am trying to plot a specific region in the arctic with ggmap. The center of the region is somewhere around lat. 80 lon. 0, unfortunately only a grey background and the latitude and longitude axis are shown. I tried to plot different regions and my code seems to work on every location, except regions which are beyond lat. 73.6. Here is an example of my code:
library(ggmap)
library(mapproj)
location <- get_map(location = c(lon = 0, lat = 80), zoom = 4, maptype = "hybrid")
ggmap(location)
So does anyone know, why ggmap is unable to plot this location?
I had the same problem (when trying to plot Antartica) and to get around it, I resorted to ggplot, but relying on a couple of functions from the ggmap package.
As #Henrik's link suggests, the map projection seems to be the problem.
The entire idea and code is courtesy of David Kahle
Here's what you can do to get it to work for your case:
location <- get_map(location = c(lon = 0, lat = 80), zoom = 4, maptype = "hybrid")
#Create a small data frame to pass to ggplot
fourCorners <- expand.grid(
lon = as.numeric(attr(location, "bb")[, c("ll.lon", "ur.lon")]),
lat = as.numeric(attr(location, "bb")[, c("ll.lat", "ur.lat")])
)
# The inset_raster function needs 4 data coordinates. Pull it out of your "location" that you got via get_map
xmin <- attr(location, "bb")$ll.lon
xmax <- attr(location, "bb")$ur.lon
ymin <- attr(location, "bb")$ll.lat
ymax <- attr(location, "bb")$ur.lat
# Now you are ready to plot it
mp <- ggplot(fourCorners, aes(x = lon, y = lat) ) +
inset_raster(location, xmin, xmax, ymin, ymax)
mp
Which gives you your "hybrid" map, centered at (Longitude=0, lat=80)
I'm very new to R and I was just playing around with a project to plot projected population growth of Alabama counties from 2010 to 2020. Here's my code:
dat <- read.table("C:/Users/rasmus/Documents/countyinfo.txt", sep="\t", header=TRUE)
library(ggplot2)
library(maps)
library(ggmap)
mdat <- map_data('county')
str(mdat)
al1 = get_map(location = c(lon = -86.304474, lat = 32.362563),
zoom = 7, maptype = 'roadmap')
al1MAP = ggmap(al1) +
geom_point(data=dat,inherit.aes = FALSE,
aes(y=Lat, x=Lon, map_id=County, size=Growth), col="red") +
borders("state", colour="red", alpha=0.8) +
borders("county", colour="blue", alpha=0.5)
al1MAP
Now, I have two questions.
1) The state borders seem to be doing weird things. Here's a screenshot with the county overlay turned off:
2) Given that this is only about Alabama, I would like to turn borders outside the state borders off, but I don't know how to do that. My guess would be to experiment with xlim and ylim, but I don't know how to restrict those to the Alabama border polygon.
It seems that with function borders() for coordinates of some states are connected together.
To solve this problem you can store state borders as separate data frame using map_data() and then add state borders using geom_path() to your map. In geom_path() add group=region to ensure that points are connected only according one region.
To show borders just for the Alabama counties you should add argument region="alabama" to function borders().
al1 = get_map(location = c(lon = -86.304474, lat = 32.362563),
zoom = 6, maptype = 'roadmap')
mdat <- map_data('state')
ggmap(al1) +
geom_path(data=mdat,aes(x=long,y=lat,group=region),colour="red",alpha=0.8)+
borders("county", colour="blue", alpha=0.5,region="alabama")