Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable hover text in plotly with ggplot

Tags:

r

ggplot2

plotly

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
like image 671
Mark R Avatar asked Dec 05 '15 17:12

Mark R


People also ask

Is Plotly faster than Ggplot?

In terms of speed, ggplot2 tends to run much slower than Plotly. With regard to integration, both Plotly and ggplot2 can integrate with a variety of tools, like Python, MATLAB, Jupyter, and React. Both Plotly and ggplot2 are good options for both large and small businesses.

What is hover name in Plotly?

Hover Labels is the most depectively-power feature for interactive visualization in plotly, for user it is the ability to reveal more information about the data points by moving the cursor (mouse) over the point and having a hover label appear.


1 Answers

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!

like image 193
bluefish Avatar answered Sep 22 '22 12:09

bluefish