Disable hover text in plotly with ggplot

2020-07-14 10:06发布

问题:

I want to partly disable hover text in plotly, limiting it to one dataframe or geom in ggplot. In the case below, I want hover only on the "cities" not the map outline. I've seen a solution in Python, but not R. And how would I control the image size to keep the map dimension right in plotly? The map demo at https://plot.ly/ggplot2/interactive-tooltip/ seems not to care!

library(mapdata)
library(ggplot2)
library(plotly)


Japan <- map_data("world2Hires", region="Japan")

Longitude <- 140
Latitude <- 36.5
df <- cbind.data.frame(Longitude,Latitude)
df$Name <- "Tokyo"
df$Name_2 <- "Tōkyō"



XX <- ggplot() + geom_polygon(data=Japan, aes(x=long, y=lat, group=group), color="black", fill="white", text="") + coord_equal() + geom_point(data=df, aes(x=Longitude, y=Latitude, text=Name), color="green")
XX 
ggplotly(XX)  ##How to get hover text only on df not Japan, and remove "[object Object]" 

XX <- ggplot() + geom_polygon(data=Japan, aes(x=long, y=lat, group=group), color="black", fill="white", text="") + coord_equal() + geom_point(data=df, aes(x=Longitude, y=Latitude, text=Name_2), color="green")
XX 
ggplotly(XX)  ##Non-Ascii text breaks hover

回答1:

First I have to admit I don't know about plotly and mapdata packages before reading your question. But these look so useful I started playing around and finally produced something useful.

I think some of your problems arising because you use the ggplotly function and not the direct plot_ly interface, which I did in my solution.

data preparation

library(mapdata)
library(ggplot2)
library(plotly)

Japan <- map_data("world2Hires", region="Japan")
Longitude <- 140
Latitude <- 36.5
df <- cbind.data.frame(Longitude,Latitude)
df$Name <- "Tokyo"
df$Name_2 <- "Tōkyō"

geo object

Here a dummy geo object is prepared. This doesn't draw anything but selects which region of the World to display. Here you could also set up the used projection.

g <- list(
  showland = F,
  coastlinewidth = 0,
  lonaxis = list(
    showgrid = TRUE,
    gridwidth = 0.5,
    range = c(125, 145.8224),
    dtick = 5
  ),
  lataxis = list(
    showgrid = TRUE,
    gridwidth = 0.5,
    range = c(25, 45.52),
    dtick = 5
  )
)

adding data

First create a scattergeo plot with only your point:

p <- plot_ly(data = df,lon = Longitude,lat = Latitude,name="City",
             text =Name_2,type = "scattergeo",mode = "markers")

Then add the cost line and disable hover info (hoverinfo = "none"):

p <- add_trace(data=Japan,lon = long,lat = lat, 
               mode = "lines",group=group,line = list(color=toRGB("black"),width = 0.5),
               type = "scattergeo",  hoverinfo = "none",showlegend = F)

Finally set the layout to the previously defined geo object:

p <- layout(p, geo = g)

Final remarks

https://plot.ly/r/reference/#layout-geo shows what parameters are available for a geo object. https://plot.ly/r/lines-on-maps/ shows a code example with some of them.

For me using Name_2 works fine and I don't know what you mean with "control the image size" you might want to ask different questions for that where you further specify what you want. Good luck!



标签: r ggplot2 plotly