Reputation: 381
I have been trying to get the following dataset to map using ggmap and ggplot2 combination.
my dataframe -> head(dt.fil) containing 855 values
lat lon airline
1 -73.9930 40.7480 Virgin America
2 -71.0200 42.3610 Virgin America
3 -118.4062 33.9454 Virgin America
4 -118.4041 33.9421 Virgin America
5 -96.9322 33.2145 US Airways
6 -118.3859 34.0220 United
my code is the following
map_airlines <- get_map(location = c(lon = mean(dt.fil$lon),
lat = mean(dt.fil$lat)), zoom = 5,
maptype = "satellite", scale = 2)
# visualze the tweet locations
ggmap(map_airlines) +
geom_point(data=dt.fil, aes(x=lon, y=lat, fill='red',
alpha=0.6, colour=airline)) +
guides(fill=FALSE, alpha=FALSE)
I get the following errors
Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: Removed 835 rows containing missing values (geom_point).
I would really appreciate if somebody could help me with this :)
Upvotes: 2
Views: 375
Reputation: 1744
There are 2 issues here:
First of all, valid latitudes go from -90 up to +90 while valid longitudes go from -180 up to +180. Seeing as your latitudes don't fall in this range, i suppose you switched them. Therefore, in the rest of this example, I changed latitude into longitude and longitude into latitude. (Seeing as you're working with American airlines and datapoints are all located in the USA, this seemed reasonable).
lon <- c(-73.9930 , -71.0200 , -118.4062 , -118.4041, -96.9322 , -118.3859 )
lat <- c(40.7480, 42.3610 , 33.9454 , 33.9421 , 33.2145 , 34.0220 )
airline <- c("Virgin America", "Virgin America", "Virgin America", "Virgin America", "US Airways", "United")
dt.fil <- data.frame(lat, lon, airline)
The second problem is that your map is not large enough for the points to be plotted. To solve this, just adjust the zoom. (i took 3 instead of 5, just adjust it untill all datapoints fit).
map_airlines <- get_map(location = c(lon = mean(dt.fil$lon),
lat = mean(dt.fil$lat)),
zoom = 3,
maptype = "satellite", scale = 2)
#visualze the tweet locations
ggmap(map_airlines) +
geom_point(data=dt.fil, aes(x=lon, y=lat, colour=airline), fill='red',
alpha=0.6) +
guides(fill=FALSE, alpha=FALSE)
Upvotes: 2