In R, using the plotly package, can I plot the map of Brazil and its states?

Asked

Viewed 2,268 times

8

I really like the package plotly to make interactive graphics. But at the time of making maps, I have not found so far a way to make the map of Brazil and its states... For example, I removed this code from the site itself

library(plotly)
df <-read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
df$hover <- paste(df$name, "Population", df$pop/1e6, " million")

df$q <- with(df, cut(pop, quantile(pop), include.lowest = T))
levels(df$q) <- paste(c("1st", "2nd", "3rd", "4th"), "Quantile")
df$q <- as.ordered(df$q)

g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showland = TRUE,
landcolor = toRGB("gray85"),
subunitwidth = 1,
countrywidth = 1,
subunitcolor = toRGB("white"),
countrycolor = toRGB("white")
)

plot_ly(df, lon = lon, lat = lat, text = hover,
    marker = list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
    color = q, type = 'scattergeo', locationmode = 'USA-states') %>%
layout(title = '2014 US city populations<br>(Click legend to toggle)', geo = g)

I made a print of the result... inserir a descrição da imagem aqui

So, I wonder if it is possible to customise my map using a shapefile can be from Brazil or some Brazilian state with some regional divisions, all provided by the shapefile itself

it is possible?

1 answer

8


Using data from the package maps you have the cities:

library(maps)
dfb<-world.cities[world.cities$country.etc=="Brazil",]
library(plotly)
dfb$hover <- paste(dfb$name, "Pop", dfb$pop/1e6, " milloes")

dfb$q <- with(dfb, cut(pop, quantile(pop), include.lowest = T))
levels(dfb$q) <- paste(c("1st", "2nd", "3rd", "4th"), "Quantile")
dfb$q <- as.ordered(dfb$q)

g <- list(
  scope = 'south america',
  #projection = list(type = 'albers usa'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  subunitwidth = 1,
  countrywidth = 1,
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white")
)

plot_ly(dfb, lon = long, lat = lat, text = hover,
        marker = list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
        color = q, type = 'scattergeo', locationmode = 'country names') %>%
  layout(title = 'Populations<br>(Click legend to toggle)', geo = g)

inserir a descrição da imagem aqui

To have the map with the states would need a database with the coordinates and information of the states to replace the dfb.

  • This is sensational... I have the shapefile of the states of Brazil, too bad we can not draw in the same way that the division between countries is made.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.