I am plotting few lat longs using ggmap and I get the output like this
I use the following code to generate this output, below code is part of the o/p
library(ggplot2)
library(data.table)
library(ggmap)
library(maps)
library(mapdata)
lat <- seq(31.26415,31.26831,0.00010)
lon <- seq(76.80890,76.82320,0.00015)
lon <- lon[seq(1,96.2)]
lon <- lon[1:42]
lat_long <- data.frame(lat,lon)
lat_median <- 31.26751
lon_median <- 76.82003
map <- get_map(location = c(lon_median, lat_median), maptype = "roadmap", zoom = 15)
ggmap(map) +
geom_path(data = lat_long, aes(), size = 2, lineend = "butt") +
geom_point(data = lat_long, color = "red3", size = 1)
My output window has lot of white space, which I could have used to show in the map. Can I increase this map output to better fit the window?
If you use the get_googlemap function you can specify the dimensions and it doesn't have to be square e.g.:
map <- get_googlemap('paris', zoom = 15, size = c(500, 200), scale = 2)
Related
I am using ggplot2 and ggmpap for plotting my co-ordinates in google map and i am not sure how to add labels for each of my coordinates.
I am using following code to plot my map
# loading the required packages
library(ggplot2)
library(ggmap)
# creating a sample data.frame with your lat/lon points
lon <- c(141.98, 141.97, 141.87, 142.05, 142.37, 142.41, 142.16, 141.99)
lat <- c(10.86, 10.99, 11.60, 11.04, 11.13, 11.63, 11.16, 11.38)
df <- as.data.frame(cbind(lon,lat))
# getting the map
register_google(key = "mykey", write = TRUE)
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom =8,
maptype = "hybrid", scale = 2)`
# plotting the map with some points on it
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, alpha = 0.2), color = "yellow", fill = "pink", size = 4, shape = 10) +
guides(fill=FALSE, alpha=FALSE, size=FALSE)
i got the map too but now i need to add labells for each of my cordinates. could someone help me in the script
Questions about map legend editing exist (e.g.), but not exactly what I need.
Using ggmap, how do I select points in a map and add annotations superimposed on the map? Take the following code:
Map <- get_map(location = 'Santiago, Chile', zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),lat=c(-34.36935,-34.29322))
Map_Points <- Map + geom_point(data = Points,aes(x=lon,y=lat,size=6))
So now I have a nice map with a few points. How do I write some annotation near one of the points?
Quite straightforward:
Code
library(ggrepel) # for the auto-repelling label
Map +
geom_point(data = Points,
aes(x = lon, y = lat),
size = 3) +
geom_label_repel(data = Points,
aes(x = lon, y = lat, label = name),
size = 3,
vjust = -2,
hjust = 1)
Data
library(tmaptools) # for the geocode lookup
library(ggmap)
santiago_coords <- rbind(as.numeric(paste(geocode_OSM("Santiago, Chile")$coords)))
Map <- get_map(location = santiago_coords, zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),
lat=c(-34.36935,-34.29322),
name=c("Location One", "Location Two"))
I'm working on various mapping of data. One of the maps of interest is taking boundary maps and overlaying them on an exist map. These existing maps may come from Google or other sources.
The issue that I'm having is that when I use geom_path(), with an alpha < 1, if two areas share a boundary, that boundary is darker than other boundaries. Any way to make all the boundaries the same transparency?
library(ggplot2)
library(ggmap)
library(maptools)
library(plyr)
zips <- readShapePoly('cb_2016_us_zcta510_500k.shp')
test <- fortify(zips)
zips#data$id <- rownames(zips#data)
zips.points <- fortify(zips)
zips.df <- join(zips.points, zips#data, by = "id")
test_zips <- c(64002,64012,64013,64014,64015,64029,64030,64034,64050
,64051,64052,64053,64054,64055,64056,64057,64063,64064,64065,64070
,64075,64080,64081,64082,64083,64086,64108,64109,64110,64111,64112
,64113,64114,64121,64123,64124,64125,64126,64127,64128,64129,64130
,64131,64132,64133,64134,64136,64137,64138,64139,64141,64145,64146
,64147,64148,64149,64170,64171,64179,64191,64198,64999,66119,66160
,66205,66206,66207,66208,66209,66211,66222,66224,66251)
Valid_Zips <- zips.df[zips.df$GEOID10 %in% test_zips,]
mapImage <- get_map(location = c(lon = -94.4, lat = 38.9),
color = "color",
source = "google",
maptype = "toner-2011",
zoom = 10)
ggmap(mapImage) +
geom_path(aes(x = long, y = lat, group = group)
,color="red",data=Valid_Zips,size=1,alpha = .3)
I am trying to plot few points on map. But I get a blank map with just the points marked in red. The map is missing
Below code is reporducible
library(maps)
library(mapdata)
library(ggplot2)
library(ggmap)
lon <- c(-122.1817252,-119.4179324,-95.7128910,-71.0588801,-81.0348144)
lat <- c(37.452960,36.778261,37.090240,42.360082,34.000710)
all_places_geocoded <- data.frame(lon,lat)
gc <- geocode('australia')
center <- as.numeric(gc)
map <- get_googlemap(location = center,maptype = "roadmap", zoom=1)
ggmap(map) +
geom_point(data = all_places_geocoded, color = "red3", size = 1)
I am plotting all latitude longitude values which I receive on a map like this.
The map has the color as per the time duration of halt recorded at that point.
My agonies are the following:
I cannot zoom in and zoom out - Centering the map is very painful - And I have 10*100 such collections to be plotted and evaluated
I can hardly make out the areas where the halt was specific, all I can see is that it was good with no halt but reality is not that
Hence I looked for plotly and to my surprise they don't have the support.
How can I achieve the results? I don't have a tool like tableau
Please suggest if you can help.
Below code is an example of what I am trying to do with ggmap :
library(ggplot2)
library(data.table)
library(ggmap)
library(maps)
library(mapdata)
lat <- seq(31.26415,31.26831,0.00010)
lon <- seq(76.80890,76.82320,0.00015)
lon <- lon[seq(1,96.2)]
lon <- lon[1:42]
lat_long <- data.frame(lat,lon)
lat_median <- 31.26751
lon_median <- 76.82003
#map <- get_map(location = c(lon_median, lat_median), maptype = "roadmap", zoom = 15)
map <- get_googlemap(center = c(lon_median, lat_median), zoom = 12, maptype = "roadmap",size = c(600, 300), scale = 2)
ggmap(map) +
geom_path(data = lat_long, aes(), size = 2, lineend = "butt") +
geom_point(data = lat_long, color = "red3", size = 1)