Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ggmap boundary based on Latitude and Longitude

So I have R program, and am struggling with getting all points in map

library(ggmap)
library(ggplot2)

setwd("d:/GIS/")
sep <- read.csv("SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])


# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] >= 50, "Over 50", "Under 50")

# get the map
map <- get_map("Kissena Park, Queens", zoom = 13, maptype = 'roadmap')

# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
  geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=9, alpha=0.6) +
  scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))

And here is output map

enter image description here

I wish to zoom in enough without cutting out data points, but no matter location I pick on map, the data keeps getting cut, i.e. Removed 2 rows containing missing values (geom_point).

Is there a way to set boundaries based on the extremities of latitude and longitude? The csv I import at

sep <- read.csv("SEP_assets_csv.csv")

Has list of latitude and longitude.

Help!

Coordinates

Latitude    Longitude
40.758365   -73.824407
40.774168   -73.818543
40.761748   -73.811379
40.765602   -73.828293
40.751762   -73.81778
40.764834   -73.789712
40.777951   -73.842932
40.76501    -73.794319
40.785959   -73.817349
40.755764   -73.799256
40.745593   -73.829283
40.789929   -73.839501
40.760072   -73.783908
40.726437   -73.807592
40.741093   -73.808757
40.720926   -73.823358
40.729642   -73.81781
40.724191   -73.80937
40.782346   -73.77844
40.778164   -73.799841
40.775122   -73.8185
40.760344   -73.817909
40.792326   -73.809516
40.78322    -73.806977
40.73106    -73.805449
40.736521   -73.813001
40.783714   -73.795027
40.770194   -73.82762
40.735855   -73.823583
40.74943    -73.82141
40.769753   -73.832001
40.754465   -73.826204
40.738775   -73.823892
40.764868   -73.826819
40.738332   -73.82028
40.735017   -73.821339
40.72535    -73.811325
40.721466   -73.820401

dput

> dput(sep)
structure(list(School = structure(1:38, .Label = c("Queens\\25Q020", 
"Queens\\25Q021", "Queens\\25Q022", "Queens\\25Q023", "Queens\\25Q024", 
"Queens\\25Q025", "Queens\\25Q029", "Queens\\25Q032", "Queens\\25Q079", 
"Queens\\25Q107", "Queens\\25Q120", "Queens\\25Q129", "Queens\\25Q130", 
"Queens\\25Q154", "Queens\\25Q163", "Queens\\25Q164", "Queens\\25Q165", 
"Queens\\25Q168", "Queens\\25Q169", "Queens\\25Q184", "Queens\\25Q185", 
"Queens\\25Q189", "Queens\\25Q193", "Queens\\25Q194", "Queens\\25Q200", 
"Queens\\25Q201", "Queens\\25Q209", "Queens\\25Q214", "Queens\\25Q219", 
"Queens\\25Q237", "Queens\\25Q242", "Queens\\25Q244", "Queens\\25Q425", 
"Queens\\25Q460", "Queens\\25Q499", "Queens\\25Q515", "Queens\\25Q707", 
"Queens\\25Q792"), class = "factor"), Latitude = c(40.758365, 
40.774168, 40.761748, 40.765602, 40.751762, 40.764834, 40.777951, 
40.76501, 40.785959, 40.755764, 40.745593, 40.789929, 40.760072, 
40.726437, 40.741093, 40.720926, 40.729642, 40.724191, 40.782346, 
40.778164, 40.775122, 40.760344, 40.792326, 40.78322, 40.73106, 
40.736521, 40.783714, 40.770194, 40.735855, 40.74943, 40.769753, 
40.754465, 40.738775, 40.764868, 40.738332, 40.735017, 40.72535, 
40.721466), Longitude = c(-73.824407, -73.818543, -73.811379, 
-73.828293, -73.81778, -73.789712, -73.842932, -73.794319, -73.817349, 
-73.799256, -73.829283, -73.839501, -73.783908, -73.807592, -73.808757, 
-73.823358, -73.81781, -73.80937, -73.77844, -73.799841, -73.8185, 
-73.817909, -73.809516, -73.806977, -73.805449, -73.813001, -73.795027, 
-73.82762, -73.823583, -73.82141, -73.832001, -73.826204, -73.823892, 
-73.826819, -73.82028, -73.821339, -73.811325, -73.820401), Windows.SEP.11 = c(48L, 
154L, 11L, 62L, 20L, 72L, 9L, 37L, 8L, 22L, 9L, 47L, 44L, 99L, 
78L, 91L, 42L, 122L, 55L, 14L, 162L, 108L, 89L, 87L, 23L, 14L, 
75L, 74L, 141L, 73L, 43L, 14L, 534L, 189L, 128L, 10L, 79L, 38L
), Mac.SEP.11 = c(49L, 0L, 180L, 2L, 202L, 116L, 41L, 1L, 17L, 
22L, 33L, 43L, 1L, 28L, 2L, 0L, 238L, 13L, 76L, 55L, 76L, 42L, 
0L, 1L, 12L, 0L, 16L, 10L, 1L, 7L, 0L, 1L, 1L, 67L, 16L, 7L, 
31L, 24L), Windows.SEP.12 = c(52L, 252L, 1L, 2L, 12L, 45L, 108L, 
15L, 14L, 4L, 19L, 21L, 46L, 90L, 10L, 86L, 15L, 76L, 122L, 2L, 
9L, 52L, 39L, 120L, 43L, 17L, 9L, 54L, 19L, 199L, 40L, 25L, 64L, 
164L, 14L, 27L, 45L, 2L), Mac.SEP.12 = c(73L, 2L, 91L, 53L, 288L, 
6L, 2L, 107L, 109L, 97L, 41L, 18L, 12L, 16L, 2L, 2L, 270L, 32L, 
45L, 92L, 54L, 190L, 1L, 4L, 19L, 53L, 1L, 10L, 0L, 61L, 50L, 
27L, 27L, 25L, 3L, 1L, 43L, 0L), newCol = c(56.3063063063063, 
62.2549019607843, 32.5088339222615, 46.218487394958, 57.4712643678161, 
21.3389121338912, 68.75, 76.25, 83.1081081081081, 69.6551724137931, 
58.8235294117647, 30.2325581395349, 56.3106796116505, 45.4935622317597, 
13.0434782608696, 49.1620111731844, 50.4424778761062, 44.4444444444444, 
56.0402684563758, 57.6687116564417, 20.9302325581395, 61.734693877551, 
31.0077519379845, 58.4905660377358, 63.9175257731959, 83.3333333333333, 
9.9009900990099, 43.2432432432432, 11.8012422360248, 76.4705882352941, 
67.6691729323308, 77.6119402985075, 14.5367412140575, 42.4719101123596, 
10.5590062111801, 62.2222222222222, 44.4444444444444, 3.125)), .Names = c("School", 
"Latitude", "Longitude", "Windows.SEP.11", "Mac.SEP.11", "Windows.SEP.12", 
"Mac.SEP.12", "newCol"), row.names = c(NA, -38L), class = "data.frame")
like image 417
Rhonda Avatar asked Jul 02 '15 15:07

Rhonda


1 Answers

You haven't provided us with any of the data, so I'm going to give an example using a dataset in the historydata package. Instead of getting a map based on a location and a zoom, you can get a map based on the bounding box of the latitudes and longitudes in your dataset.

library(historydata)
library(ggmap)
data("catholic_dioceses")

bbox <- make_bbox(catholic_dioceses$long, catholic_dioceses$lat, f = 0.01)
map <- get_map(bbox)

ggmap(map) +
  geom_point(data=catholic_dioceses, aes(x = long, y = lat))

Note that the f = argument to make_bbox() lets you control how much padding there is around your map.

In your case, I think this will work:

library(ggmap)

bbox <- make_bbox(sep$Longitude, sep$Latitude, f = 0.01)
map <- get_map(bbox)

ggmap(map) +
  geom_point(data=sep, aes(x = Longitude, y = Latitude, 
                           color = Percent_SEP12_Assets),
                           size = 9, alpha = 0.6) +
  scale_color_manual(breaks=c("Over 50", "Under 50"), values=c("green","red"))
like image 88
Lincoln Mullen Avatar answered Sep 30 '22 06:09

Lincoln Mullen