0

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.

enter image description here

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

pacomet
  • 5,011
  • 12
  • 59
  • 111
  • Please add the output of `dput(head(mapa_df2))`. – ismirsehregal Jan 28 '21 at 09:05
  • How about your second df `mapa.prov_df2`? is `nombre` also a column of this dataset? – stefan Jan 28 '21 at 20:16
  • @stefan Yes `nombre` is 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. – pacomet Jan 29 '21 at 07:37
  • When I run your code without the `geom_polygon` (which uses `mapa.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 named `nombre` which will raise the same error message you got. – stefan Jan 29 '21 at 07:53
  • Yes, the second data set is the provincial border just to overlay. The error message I get is `Error in FUN(X[[i]], ...) : object 'nombre' not found`. If I remove ` text = nombre` from the ggplot it works fine but the tooltip shows `trace : 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. – pacomet Jan 29 '21 at 08:14
  • @stefan just added a graph to show the result I'm getting now where I would like to show `nombre` – pacomet Jan 29 '21 at 08:24
  • Hi pacomet. Maybe it's a different issue. But I'm quite sure that the issue is related to `map_prov.df2`. When I exclude the 'geom_path` your code runs fine on my machine and gives me a tooltip with the name of the province. The same is true if I simply replace `map_prov.df2` by `map.df`. Unfortunately I could not check that as you provide no information about this dataset. – stefan Jan 29 '21 at 10:13
  • @stefan added link to csv file for `mapa_prov.df2`. nombre variable is not present here, related to the error message – pacomet Jan 29 '21 at 10:19

1 Answers1

2

(: "nombre variable is not present here"? That was the question I asked in my first comment and the reason for the error. Every variable which you put in ggplot(aes(...)) has to be present in each dataframe, i.e. these are global aesthetics. Hence, you get an error message when adding mapa_prov.df2 as ggplot2 looks for a variable nombre in this dataset too. To prevent that you have to make text a local aes of the geom_polygon, i.e. do geom_polygon(color = "white",aes(fill = nivel1, text=nombre)).

library(plotly) 

mapa_df2 <- readr::read_csv("CSV_data.csv")
mapa.prov_df2 <- readr::read_csv("mapa_prov.csv")

ggplot(mapa_df2, aes(x = long, y = lat, group = group)) +
  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, text = nombre)) +
  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(width = 748, height = 1024, tooltip = "text")

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • That was my mistake, thought the data frame were independent when plotting as they were called separately. Now it is working fine. Thanks. – pacomet Jan 29 '21 at 10:40