Skip to content

Instantly share code, notes, and snippets.

@amitkaps
Created September 4, 2015 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitkaps/6ae202b799a07eb259fb to your computer and use it in GitHub Desktop.
Save amitkaps/6ae202b799a07eb259fb to your computer and use it in GitHub Desktop.
# Load the library
library(ggplot2)
library(ggmap)
# Create a dummy data frame of points
set.seed(500)
df <- round(data.frame(
lon = jitter(rep( 77.59, 50), amount = .3),
lat = jitter(rep( 12.97, 50), amount = .3)
), digits = 2)
View(df)
# Just plotting the points
ggplot(df) + aes(lon, lat) + geom_point()
# Plotting with coordinate system for map
ggplot(df) + aes(lon, lat) + geom_point() + coord_map("mercator")
# Get lat and lon for a place
geocode("Bangalore")
# Get a map
map <- get_map("Bangalore")
ggmap(map)
# You can get other maps - Open Street Map, Stamen etc.
map1 <- get_map("Bangalore", maptype = "toner", source = "stamen")
ggmap(map1)
# Plot the data
ggmap(map1) + geom_point(data = df, aes(lon, lat),
color = "red", size = 6)
# Plot a Choropleth
library(choroplethr)
library(scales)
str(df_pop_state)
View(df_pop_state)
ggplot(df_pop_state) + aes(reorder(region,value), weight = value) +
geom_bar() + coord_flip() + scale_y_continuous(labels = comma)
# Get map shapes
library(maps)
map("state")
states <- map_data("state")
# Combine the data set and create choropleth
choro <- merge(states, df_pop_state, sort = FALSE, by = "region")
ggplot(choro) + aes(long, lat, group = group, fill = value) +
geom_polygon() + coord_map()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment