我想在 plotly 中部分禁用悬停文本,将其限制为 ggplot 中的一个数据框或 geom.在下面的情况下,我只想将鼠标悬停在城市"而不是地图轮廓上.我在 Python 中看到了一个解决方案,但不是 R.我将如何控制图像大小以保持地图尺寸正确?https://plot.ly/ggplot2/interactive-tooltip/ 的地图演示似乎不是关心!
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
首先我必须承认,在阅读您的问题之前,我不了解 plotly 和 mapdata 包.但是这些看起来非常有用,我开始玩弄并最终产生了一些有用的东西.
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.
我认为你的一些问题是因为你使用 ggplotly
函数而不是直接 plot_ly
接口,我在我的解决方案中这样做了.
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.
数据准备
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ō"
地理对象
这里准备了一个虚拟地理对象.这不会绘制任何东西,而是选择要显示的 World 的哪个区域.在这里您还可以设置使用的投影.
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
)
)
添加数据
首先用你的观点创建一个 scattergeo
图:
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")
然后添加成本行并禁用悬停信息(hoverinfo = "none"
):
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)
最后将布局设置为之前定义的geo对象:
Finally set the layout to the previously defined geo object:
p <- layout(p, geo = g)
最后的评论
https://plot.ly/r/reference/#layout-geo 显示可用于地理对象的参数.https://plot.ly/r/lines-on-maps/ 显示一个其中一些的代码示例.
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.
对我来说,使用 Name_2
效果很好,我不知道您对控制图像大小"的意思是什么,您可能想针对您进一步指定所需内容提出不同的问题.祝你好运!
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!
上一篇:prevent反病毒软件来阻止C#传出的电子邮件反病毒软件、电子邮件、prevent
下一篇:调用.ToArray当ArgumentException的()ToArray、ArgumentException