I've managed to plot a map where each municipality is filled with a different color depending on the value of a column in the data frame. This is the code I'm using for the plot:
mapa_df2 %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(color = "black",aes(fill = nivel1)) +
coord_map("mercator") +
labs(title = "Day 1 level map") +
scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
limits=c(0,3))
Now I'm trying to convert to an interactive map with plotly.
p <- mapa_df2 %>%
ggplot(aes(x = long, y = lat, group = group, text = nombre)) +
geom_path(data=mapa.prov_df2, aes(x=long, y=lat, group=group),color="blue", size=1.5) +
geom_polygon(color = "white",aes(fill = nivel1)) +
coord_map("mercator") +
labs(title = "Day 1 level map") +
scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
limits=c(0,3))
ggplotly(p, width = 748, height = 1024, tooltip = "text")
Then I get an error message: Error in FUN(X[[i]], ...) : object 'nombre' not found but nombre is one of the columns in data frame mapa_df2, it actually exists.
I would like that the name of the municipality appears when the mouse is over. If I remove text=nombre from the code above then I get trace: value in the hover message. For sure there is a mistake in the tooltip or text=. Any hint or idea will be very welcome.
Output of head(mapa_df2)
long lat order hole piece id group nombre_municipio c_autonoma provincia municipio zona nombre
1 -0.1963169 38.85944 2421544 FALSE 1 34100303001 34100303001.1 l'Atzúbia 10 03 03001 20 Adsubia
2 -0.1959364 38.86003 2421545 FALSE 1 34100303001 34100303001.1 l'Atzúbia 10 03 03001 20 Adsubia
3 -0.1957341 38.86049 2421546 FALSE 1 34100303001 34100303001.1 l'Atzúbia 10 03 03001 20 Adsubia
4 -0.1957434 38.86084 2421547 FALSE 1 34100303001 34100303001.1 l'Atzúbia 10 03 03001 20 Adsubia
5 -0.1976370 38.86632 2421548 FALSE 1 34100303001 34100303001.1 l'Atzúbia 10 03 03001 20 Adsubia
6 -0.1939176 38.86917 2421549 FALSE 1 34100303001 34100303001.1 l'Atzúbia 10 03 03001 20 Adsubia
nivel1 nivel2 nivel3
1 1 1 1
2 1 1 1
3 1 1 1
4 1 1 1
5 1 1 1
6 1 1 1
EDIT 1 As the output of dput was too large, just added a link to sample data used in the script Link to sample data
EDIT 2
Graphical output. Where the label says 'trace 0' I would like to replace with the column nombre which would show the name of the municipality.
EDIT 3
As @stefan said, running without the call geom_path and with some changes the ggplot runs fine
p <- mapa_df2 %>%
ggplot(aes(x = long, y = lat, group = group, fill = nivel1,
text = nombre)) +
geom_polygon(color = "white") +
coord_map("mercator") +
scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
limits=c(0,3))
But now I need to find how to overlay mapa_prov.df2 without affecting the hover
EDIT 4
Link to data in mapa_prov.df2 mapa_prov.csv


dput(head(mapa_df2)).mapa.prov_df2? isnombrealso a column of this dataset?nombreis a column in the dataset. It is the name of the municipality I would like to show when the mouse is over, filled with the color of the polygon.geom_polygon(which usesmapa.prov_df2) it works fine. I don't get any errors. As you haven't provided the second dataset I guessed that this second dataset does not include a column namednombrewhich will raise the same error message you got.Error in FUN(X[[i]], ...) : object 'nombre' not found. If I remove ` text = nombre` from the ggplot it works fine but the tooltip showstrace : value, being value o to 3 the value assigned to each polygon. map_prov.df2 can be removed from the plot, it has nothing to do with the tooltip issue, I think.