[英]How can I remove the size line in the hoverinfo of a Plotly chart in R?
I've found the following page instructing how to create custom hover text for plotly charts in R. 我发现以下页面指示如何为R中的绘图图表创建自定义悬停文本。
https://plot.ly/r/text-and-annotations/#custom-hover-text https://plot.ly/r/text-and-annotations/#custom-hover-text
This seems to do exactly what I want, however when I copy the code (see below) into RStudio and run it locally I get an extra line in in my hoverinfo, showing the size variable. 这似乎完全符合我的要求,但是,当我将代码(见下文)复制到RStudio中并在本地运行时,我的hoverinfo中又多了一行,显示了size变量。
Screenshot of the chart in RStudio: RStudio中图表的屏幕截图:
How can I remove this "wt (size): 1.835" line in hoverinfo? 如何在hoverinfo中删除此“ wt(size):1.835”行?
library(plotly)
p <- mtcars %>%
plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, size = wt,
hoverinfo = "text",
text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) %>%
layout(title ="Custom Hover Text")
p
I can achieve what you want, but it's ugly, and really a bit of a hack. 我可以实现您想要的功能,但这很丑陋,而且确实有点hack。 I'm not overly proud of this but here we go. 我对此并不感到骄傲,但是我们开始吧。
# Your plot
library(plotly)
p <- mtcars %>%
plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, size = wt,
hoverinfo = "text",
text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg)) %>%
layout(title ="Custom Hover Text")
p
# Get the list for the plot
pp <- plotly_build(p)
# Pick up the hover text
hvrtext <- pp$data[[1]]$text
# Split by line break and wt
hvrtext_fixed <- strsplit(hvrtext, split = '<br>wt')
# Get the first element of each split
hvrtext_fixed <- lapply(hvrtext_fixed, function(x) x[1])
# Convert back to vector
hvrtext_fixed <- as.character(hvrtext_fixed)
# Assign as hovertext in the plot
pp$data[[1]]$text <- hvrtext_fixed
# Plot
pp
I came here looking for same solution and the above one worked after some haggle but I ultimately found the right method later. 我来到这里寻找相同的解决方案,但经过一番讨价还价,以上解决方案仍然有效,但后来我最终找到了正确的方法。 Here it is: 这里是:
Put your 'Size' variable inside marker=list() 将您的“大小”变量放入marker = list()
So instead of 所以代替
plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, size = wt,
hoverinfo = "text",
text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg))
You can use 您可以使用
plot_ly(x = disp, y = mpg, mode = "markers", color = cyl, marker=list(size=wt),
hoverinfo = "text",
text = paste("Displacement = ", mtcars$disp, "Miles Per Gallon = ", mtcars$mpg))
That worked for me. 那对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.