Disable hover text in plot using ggplot

I want to partially disable the hover text in the plot, limiting it to a single data file or geom in ggplot. In the case below, I want to move the cursor only to the "cities", and not to the map plan. I saw the solution in Python, but not R. And how could I control the size of the image to keep the map size right in the plot? The map demo at https://plot.ly/ggplot2/interactive-tooltip/ doesn't seem 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
+4
source share
1 answer

, , , , plotly mapdata. , , , - .

, - ggplotly, plot_ly, .

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ō"

-

. , , . .

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
  )
)

scattergeo :

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

hover (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)

, :

p <- layout(p, geo = g)

https://plot.ly/r/reference/#layout-geo , . https://plot.ly/r/lines-on-maps/ .

Name_2 , , " ", , , . !

+2

Source: https://habr.com/ru/post/1618718/


All Articles