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

R plotly: Customize Hover (Info and Text)

我想自定义悬停在栏上时在 plotly 中看到的内容。

请查看 post 末尾的 reprex。 我看过

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

但我一定是犯了一些错误。 我想要的是当我将鼠标悬停在栏上时只看到变量“macro_sector”和“数量”,而栏上根本没有静态文本。 我怎样才能做到这一点?非常感谢

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
                       ) 

reprex package (v2.0.1)

创建于 2022-02-08

hoverinfo参数"text"需要引用:

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:

Sets text elements associated with each (x,y) pair. If a single string, the same string appears over all the data points. If an array of string, the items are mapped in order to the this trace's (x,y) coordinates. If trace hoverinfo contains a "text" flag and "hovertext" is not set, these elements will be seen in the hover labels.

hovertext:

Sets hover text elements associated with each (x,y) pair. If a single string, the same string appears over all the data points. If an array of string, the items are mapped in order to the this trace's (x,y) coordinates. To be seen, trace hoverinfo must contain a "text" flag.