Disable hover text in plotly with ggplot

假装没事ソ 提交于 2019-12-04 09:51:43

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!