Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

How to create county level chloropeth maps wih plotly in R

Hello i have this code which produces chloropeth map in state level with plotly but i want to produce exactly the same map in counties level. I tried to change the locationmode = ‘USA-states’ to locationmode = ‘USA-counties’ but it does not work. Any ideas? (emergency)

library(plotly)
df <- read.csv(“https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv”)
df$hover <- with(df, paste(state, ‘
’, “Beef”, beef, “Dairy”, dairy, “
”,
“Fruits”, total.fruits, “Veggies”, total.veggies,
"
", “Wheat”, wheat, “Corn”, corn))

give state boundaries a white border

l <- list(color = toRGB(“white”), width = 2)

specify some map projection/options

g <- list(
scope = ‘usa’,
projection = list(type = ‘albers usa’),
showlakes = TRUE,
lakecolor = toRGB(‘white’)
)

p <- plot_geo(df, locationmode = ‘USA-states’) %>%
add_trace(
z = ~total.exports, text = ~hover, locations = ~code,
color = ~total.exports, colors = ‘Purples’
) %>%
colorbar(title = “Millions USD”) %>%
layout(
title = ‘2011 US Agriculture Exports by State
(Hover for breakdown)’,
geo = g
)

@makis23 Take a look at this :+1:

I have already done but i want exactly the same map visualization with this
one https://plot.ly/r/choropleth-maps/

Hey @makis23,

You can simply expand the scale/map from the above example. For example,

library(plotly)
library(maps)

county_df <- map_data("county")
state_df <- map_data("state")

# create state boundaries
p <- ggplot(county_df, aes(long, lat, group = group)) +
  geom_polygon(colour = alpha("black", 1/2), fill = NA) +
  geom_polygon(data = state_df, colour = "black", fill = NA) + 
  theme_void()

p <- ggplotly(p)
p 

For more examples see https://plot.ly/~bdun9/1289.embed and https://plot.ly/ggplot2/geom_polygon/

When i do this i lose all the boundaries of state and counties

library(plotly)
library(maps)

county_df <- map_data(“county”)
state_df <- map_data(“state”)

create state boundaries

p1 <- ggplot(foo1_eqi, aes(long, lat, group = group)) +
geom_polygon(aes(fill=EQI, group=subregion)) +
theme_void()

p1 <- ggplotly(p1)
p1

Actually this my dataset. I have already applied map_data(“county”) on it.
head(foo1_eqi)
subregion long lat group order region EQI County_ID
1 abbeville -82.24809 34.21131 2285 67187 south carolina 0.064558 45001
2 abbeville -82.27674 34.10818 2285 67190 south carolina 0.064558 45001
3 abbeville -82.23663 34.18266 2285 67188 south carolina 0.064558 45001
4 abbeville -82.24236 34.15401 2285 67189 south carolina 0.064558 45001
5 abbeville -82.30538 34.08526 2285 67191 south carolina 0.064558 45001
6 abbeville -82.60332 34.05088 2285 67203 south carolina 0.064558 45001
air_EQI water_EQI land_EQI sociod_EQI air_EQI.1 built_EQI good_days
1 0.3634782 0.9929249 -0.0150723 -0.2205935 0.3634782 -0.4392307 12.37
2 0.3634782 0.9929249 -0.0150723 -0.2205935 0.3634782 -0.4392307 12.37
3 0.3634782 0.9929249 -0.0150723 -0.2205935 0.3634782 -0.4392307 12.37
4 0.3634782 0.9929249 -0.0150723 -0.2205935 0.3634782 -0.4392307 12.37
5 0.3634782 0.9929249 -0.0150723 -0.2205935 0.3634782 -0.4392307 12.37
6 0.3634782 0.9929249 -0.0150723 -0.2205935 0.3634782 -0.4392307 12.37
bad_days
1 2.3
2 2.3
3 2.3
4 2.3
5 2.3
6 2.3

i use this code but i do not take any boundaries neither for counties nor for states
p<-ggplot(foo1_eqi, aes(x = long, y = lat, group = group, fill = EQI))+
geom_polygon()
p
ggplotly§

I did this with:
output$plot1 <- renderPlotly ({

ggplot(foo1_eqi, aes(x=long, y=lat, group=group))+ 
  geom_polygon(colour=alpha("black", 1/2))+
  theme_void()

})

but is there a way to give info about Count_ID and subregion simulatenously (based on my df above) when the user makes a mouse hover on a specific area of the map? How should i use the fill option?