用ggplot积极禁用悬停文本 [英] Disable hover text in plotly with ggplot

查看:230
本文介绍了用ggplot积极禁用悬停文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部分禁用悬停文本,将其限制为ggplot中的一个数据框或几何。在下面的情况下,我只想在城市而不是地图轮廓上悬停。我已经看到了Python中的一个解决方案,但没有看到R.我如何控制图像大小以保持地图尺寸的正确性? https://plot.ly/ggplot2/interactive-tooltip/ 上的地图演示似乎不是照顾!

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


日本< - map_data(world2Hires,region =Japan)

经度< - 140
纬度< - 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)##如何获得悬停文本只在df不是日本,并删除[对象对象]

XX < - ggplot()+ geom_polygon(data = Japan,aes(x = long,y = lat,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)##非Ascii文字分页悬停


解决方案

首先,我必须承认,在阅读您的问题之前,我不知道有关plotly和mapdata的软件包。但是,这些看起来非常有用,我开始玩,最后产生了一些有用的东西



我想你的一些问题是因为你使用 ggplotly 函数而不是直接的 plot_ly 界面,这是我在我的解决方案中完成的。

数据准备

  library( mapdata)
图书馆(ggplot2)
图书馆(绘图)

日本< - map_data(world2Hires,region =Japan)
经度< - 140
Latitude < - 36.5
df < - cbind.data.frame(经度,纬度)
df $名称< - Tokyo
df $ Name_2< - Tōkyō

地理对象



这里准备了一个虚拟地理对象。这不会画任何东西,而是选择显示世界的哪个区域。在这里你可以设置使用的投影。

  g < - 列表(
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)

然后添加成本行并禁用悬停信息( hoverinfo =none):

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

最后将布局设置为之前定义的geo object:

  p < -  layout(p,geo = g)



最后的评论

https://plot.ly/r/reference/#layout-geo 显示了地理对象可用的参数。 https://plot.ly/r/lines-on-maps/ 显示一个其中一些代码示例。



对于我来说,使用 Name_2 可以正常工作,但我不知道控制图像大小您可能想要针对您进一步指定您想要的内容提出不同的问题。祝你好运!

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

解决方案

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!

这篇关于用ggplot积极禁用悬停文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆