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!
Related
I'm trying to add a 20km scale bar to my map but every solution I've tried either adds the scale bar off screen (you can only see the bottom of the "km") or doesn't add it at all.
The closest I've come has been using scalebar(), which adds a scale bar off screen but doesn't allow me to move it to be fully visible. I've also tried making a bar from scratch with geom_line etc but that did not plot at all.
Here is a reproducible map without an attempt to make a scale bar and a small set of coordinates
library(ggmap)
library(ggsn)
wd <- getwd()
Latitude <- c(34.1365, 34.14435, 34.05111, 34.17605)
Longitude <- c(-117.92391, -117.85036, -118.31712, -118.31712)
graphingdata <- cbind.data.frame(Latitude,Longitude)
# compute the bounding box
bc_bbox <- make_bbox(lat = as.numeric(graphingdata$Latitude), lon = as.numeric(graphingdata$Longitude))
bc_bbox
# grab the map from google
site_map <- get_stamenmap(bc_bbox, zoom = 10, maptype = "terrain")
#create and save the map
png(file=paste0(wd,"stack-ex-graph.png"))
map <- ggmap(site_map, legend = "bottom") +
geom_point(data = graphingdata, aes(x = as.numeric(Longitude),
y = as.numeric(Latitude)), color = "red", size = 2) +
ggtitle(paste0("This is the map title"),
subtitle = paste0("This is the subtitle"))
print(map)
dev.off()
I ended up being able to use the anchor argument to shift the location of the scale bar. Because of the scope of the project I made the anchor and bounding of the map flexible.
#Create the data frame
Latitude <- c(34.1365, 34.14435, 34.05111, 34.17605)
Longitude <- c(-117.92391, -117.85036, -118.31712, -118.31712)
graphingdata <- cbind.data.frame(Latitude,Longitude)
#Set up bounding box
height <- max(graphingdata$Latitude) - min(graphingdata$Latitude)
width <- max(graphingdata$Longitude) - min(graphingdata$Longitude)
sac_borders <- c(bottom = min(graphingdata$Latitude) - 0.1 * height,
top = max(graphingdata$Latitude) + 0.1 * height,
left = min(graphingdata$Longitude) - 0.1 * width,
right = max(graphingdata$Longitude) + 0.1 * width)
#Get the site map
map <- get_stamenmap(sac_borders, zoom = 10, maptype = "terrain")
map <- ggmap(site_map, legend = "bottom")+
scalebar(x.min = sac_borders[[3]]-1, x.max = sac_borders[[4]]+1,y.min = sac_borders[[1]]-1, y.max = sac_borders[[2]]+1,transform = TRUE,
dist = 20,dist_unit = "km",model = "WGS84",anchor = c(x=sac_borders[[3]]+0.6,y=sac_borders[[1]]+0.25), st.size = 2, border.size = 0.5)+
geom_point(data = graphingdata, aes(x = as.numeric(Longitude),
y = as.numeric(Latitude)), color = "red", size = 2) +
ggtitle(paste0("This is the map title"),
subtitle = paste0("This is the subtitle"))
Example output map. Scale bar can be moved with the anchor argument. Zoom can be modified in get_stamenmap().
I am generating a map very similar to this
(source: dominodatalab.com)
] to visualize the frequency in which cities occur. How can I create a legend or key for this map so I can see what the size of the circles represent numerically?
Here is my code:
myLocation <- c(-124, 32, -66, 42)
myMap <- get_map(location = myLocation, source = "stamen",
maptype = "toner", crop = FALSE, zoom = 4)
ggmap(myMap) +
geom_point(aes(x = lon, y = lat), data = csr.Ax1,
col = "orange", alpha = 0.4, size = csr.Ax1$freq*.25) +
scale_size_continuous(range = range(csr.Ax1$freq), guide = "legend") +
ggtitle("CSR Active Candidates") +
legend()
EDIT: SOLVED!
The solution was placing the size=csr.Ax1$freq from the geom_point argument into the aes() argument.
The solution was placing the size=csr.Ax1$freq from the geom_point argument into the aes() argument.
I would like to create a map that is not perfectly square but rectangular and is the size I dictate.
require(ggmap)
tenmile <- get_map(location = c(lon = -122.486328, lat = 48.862813),
color = "color",
source = "google",
maptype = "roadmap",
zoom = 12)
tenmile.map <- ggmap(tenmile,
extent = "device",
ylab = "Latitude",
xlab = "Longitude")+ggtitle("GEOMean for Data from Oct 2013-Nov 2014")
tenmile.map + geom_point(data=pp, aes(x=lon, y=lat, size=geomean), color="red", alpha=0.5) +
geom_text(data=pp, aes(x=lon, y=lat, label = site), size=3, vjust = 1.25, hjust = -0.1)
I would post pictures of what I get and what I want but I do not have enough reputation points to post images. =-(
Sandy Muspratt's answer produces a rectangular map, but it gets stretched. To get an unstretched map, ratio must be adjusted to the ratio between spacing of parallels and meridians at the place of the map. That is:
ratio = 1/cos(latitude)
If latitude is given in degrees, that becomes:
ratio = 1/cos(pi*latitude/180)
I give here an example using a map of Barcelona (Barcelona makes a good example to check for stretching because most of our streets form an square grid and deformation becomes easily noticeable).
library(ggmap) library(mapproj) mapbcn <- get_map(location =
'Barcelona, Catalonia', zoom = 13)
# square map (default) ggmap(mapbcn)
# map cropped by latitude
ggmap(mapbcn) +
coord_fixed(ylim=c(41.36,41.41),
ratio=1/cos(pi*41.39/180))
# map cropped by longitude
ggmap(mapbcn) +
coord_fixed(xlim=c(2.14, 2.18),
ratio=1/cos(pi*41.39/180))
It must be noted that this way coordinates keep working for the whole map (for example to add points to the map) if the area of the map is small enough not to take in account Earth's curvature - that is, to assume that meridians are parallel in the area shown by the map. It may be inaccurate in a map spanning some hundreds of kilometres and very wrong in a continent-scale map.
If you want to keep the original limits of the bounding box but simply to change its shape, you can adjust the aspect ratio. If you want to change the limits of the bounding box, then obtain the map as before but set its limits using coord_fixed() (or coord_cartesian()). Or you can adjust both the aspect ratio and the limits of the bounding box.
tenmile <- get_map(location = c(lon = -122.486328, lat = 48.862813),
color = "color",
source = "google",
maptype = "roadmap",
zoom = 12)
tenmile.map <- ggmap(tenmile,
ylab = "Latitude",
xlab = "Longitude")+ggtitle("GEOMean for Data from Oct 2013-Nov 2014") +
coord_fixed(xlim = c(-122.55, -122.40), ratio = 2/1)
This problem came up previously, and as suggested here, it was addressed. But it seems to have been only partially addressed.
First, here is code that works, showing a dot approximately at the location of the White House:
require(ggmap)
gc = geocode('the white house')
qmap(location = "the white house", maptype = "roadmap", zoom = 3) +
geom_point(aes(x = lon, y = lat), data = gc, color="red", size = 3)
However, if I change maptype to "toner", which I prefer for its simplicity and clarity of state boundaries, the White House goes north, well into Canada:
qmap(location = "the white house", maptype = "toner", zoom = 3) +
geom_point(aes(x = lon, y = lat), data = gc, color="red", size = 3)
How can I fix this?
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")