R plotly:自定义悬停(信息和文本)

4
我希望能够自定义在 plotly 上悬停在柱状图上时所看到的内容。
请查看本帖子末尾的 reprex。 我查看了 如何设置不同的文本和 hoverinfo 文本

https://community.rstudio.com/t/changing-hovertext-in-plotly/71736

但我一定犯了一些错误。当我悬停在一个条形图上时,我想只看到变量“macro_sector”和“amount”,而不是在条形图上看到任何静态文本。我该怎么做?非常感谢。
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout


df <- structure(list(macro_sector = c("U Activities of extraterritorial organisations and bodies", 
"T Activities of households as employers; u0ndifferentiated goods- and services-producing activities of households for own use", 
"P Education", "O Public administration and defence; compulsory social security", 
"K Financial and insurance activities", "S Other service activities", 
"A Agriculture, forestry and fishing", "L Real estate activities", 
"Q Human health and social work activities", "B Mining and quarrying ", 
"E Water supply; sewerage, waste management and remediation activities", 
"R Arts, entertainment and recreation", "F Construction", "I Accommodation and food service activities", 
"M Professional, scientific and technical activities", "J Information and communication", 
"N Administrative and support service activities", "G Wholesale and retail trade; repair of motor vehicles and motorcycles", 
"D Electricity, gas, steam and air conditioning supply", "H Transportation and storage", 
"C Manufacturing"), amount = c(2.2935, 17.6690005, 204.47243683, 
347.24800021, 400.12333973, 613.6673475, 657.3337123, 672.74723757, 
736.05549391, 828.06106689, 961.82905534, 1538.40117934, 1918.77325865, 
2397.02875187, 3941.39973575, 4735.36088945, 5025.39834121, 5913.94010435, 
10267.76691901, 12407.11032805, 35782.07991161), macro_sector1 = structure(1:21, .Label = c("U", 
"T", "P", "O", "K", "S", "A", "L", "Q", "B", "E", "R", "F", "I", 
"M", "J", "N", "G", "D", "H", "C"), class = "factor")), class = c("tbl_df", 
                                                                  "tbl", "data.frame"), row.names = c(NA, -21L)) %>%
    mutate(amount=round(amount, 2))

       fig <-  plot_ly(df,
                       x = ~amount, y = ~macro_sector1,
                       type = 'bar' ,
                       text= ~paste0(macro_sector, "<br>", amount),
                       hoverinfo= text ## ,
                       ## hovertext = text
                       ) 

2022年2月8日由 reprex package (v2.0.1)创建

1个回答

5

"text"参数需要用引号括起来,如:hoverinfo

library(dplyr)
library(plotly)

DF <- structure(list(macro_sector = c("U Activities of extraterritorial
                     organisations and bodies", "T Activities of households as employers;
                     u0ndifferentiated goods- and services-producing activities of households for
                     own use", "P Education", "O Public administration and defence; compulsory
                     social security", "K Financial and insurance activities", "S Other service
                     activities", "A Agriculture, forestry and fishing", "L Real estate
                     activities", "Q Human health and social work activities", "B Mining and
                     quarrying ", "E Water supply; sewerage, waste management and remediation
                     activities", "R Arts, entertainment and recreation", "F Construction", "I
                     Accommodation and food service activities", "M Professional, scientific and
                     technical activities", "J Information and communication", "N Administrative
                     and support service activities", "G Wholesale and retail trade; repair of
                     motor vehicles and motorcycles", "D Electricity, gas, steam and air
                     conditioning supply", "H Transportation and storage", "C Manufacturing"),
                     amount = c(2.2935, 17.6690005, 204.47243683, 347.24800021, 400.12333973,
                     613.6673475, 657.3337123, 672.74723757, 736.05549391, 828.06106689,
                     961.82905534, 1538.40117934, 1918.77325865, 2397.02875187, 3941.39973575,
                     4735.36088945, 5025.39834121, 5913.94010435, 10267.76691901, 12407.11032805,
                     35782.07991161), macro_sector1 = structure(1:21, .Label = c("U", "T", "P",
                     "O", "K", "S", "A", "L", "Q", "B", "E", "R", "F", "I", "M", "J", "N", "G",
                     "D", "H", "C"), class = "factor")), class = c("tbl_df", "tbl", "data.frame"),
                     row.names = c(NA, -21L)) %>% mutate(amount=round(amount, 2))

fig <-  plot_ly(DF,
                x = ~ amount,
                y = ~ macro_sector1,
                type = 'bar',
                hovertext = ~ paste0(macro_sector, "<br>", amount),
                hoverinfo = "text")
fig

结果

来自文档:

text

设置与每个(x,y)对应的文本元素。如果是单个字符串,则该字符串将出现在所有数据点上。如果是字符串数组,则按顺序将这些项目映射到此跟踪的(x,y)坐标。如果跟踪hoverinfo包含“text”标志并且未设置“hovertext”,则会在悬停标签中看到这些元素。

hovertext

设置与每个(x,y)对应的悬停文本元素。如果是单个字符串,则该字符串将出现在所有数据点上。如果是字符串数组,则按顺序将这些项目映射到此跟踪的(x,y)坐标。要看到它们,跟踪hoverinfo必须包含“text”标志。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接