Reputation: 349
I'm trying to use the plotly package to create visualizations on the data of Tamil Nadu Districts in India.
I'm able to create such as the one below data from here:
code for it below
library(plotly)
df <- tamil_nadu__.india_._lat.long.alt
df$hover <- paste(df$name, "Altitude", df$Altitude_in_ft, "Altitude in Feet")
df$q <- with(df, cut(Altitude_in_ft, quantile(Altitude_in_ft)))
levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
df$q <- as.ordered(df$q)
g <- list(
scope = "asia",
showland = TRUE,
landcolor = toRGB("gray85"),
subunitwidth = 1,
countrywidth = 1,
subunitcolor = toRGB("white"),
countrycolor = toRGB("white")
)
plot_ly(df, lon = Lon, lat = Lat, text = hover,color = q, type = 'scattergeo') %>%
layout(geo = g)
`
However, I don't have district/city or state outlines available. Plotly provides this only for USA.
I do have access to Shape files from http://www.gadm.org/country
Is it possible to import a shape file and use map plots from plotly?
Upvotes: 2
Views: 2756
Reputation: 2832
Have you thought about incorporating leaflet? For example, this code will give you a map of the region:
install.packages("leaflet")
library(leaflet)
leaflet() %>% addTiles() %>%
setView(lng = 78, lat = 10.5, zoom = 6)
And then you can use either addPolygons()
or addPolylines()
to incorporate the data from gadm.org., with the argument data
, e.g.:
leaflet() %>% addTiles() %>%
setView(lng = 78, lat = 10.5, zoom = 6) %>%
addPolylines(data = YOUR_DATA, color = "blue", weight = 3)
Upvotes: 0
Reputation: 9886
to follow-up with my comment, here is an alternative (I don't know if it's possible with plotly) that could give you want you want. I downloaded the shapefile from your link, set the projection and plot. I don't have access to your dataset but your would only need to add addCircles() or addMarkerCircles() to the expression below.
library(maptools)
library(leaflet)
library(htmltools)
library(htmlwidgets)
IND_adm1 <- readShapeSpatial("pathtoyour/IND_adm1.shp")
proj4string(IND_adm1) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
str(IND_adm1@data)
factpal <- colorFactor(topo.colors(36), IND_adm1$NAME_1)
leaflet() %>% addProviderTiles("Esri.WorldImagery") %>%
addPolygons(data=IND_adm1, color= ~factpal(NAME_1))
Upvotes: 2