我想自定义当我将鼠标悬停在栏上时我在情节中看到的内容。
请查看帖子末尾的 reprex。我看了一下
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
)
Run Code Online (Sandbox Code Playgroud)
由reprex 包于 2022 年 2 月 8 日创建(v2.0.1)
该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
Run Code Online (Sandbox Code Playgroud)
来自文档:
文本:
设置与每个 (x,y) 对关联的文本元素。如果是单个字符串,则相同的字符串出现在所有数据点上。如果是字符串数组,则项目将按顺序映射到该迹线的 (x,y) 坐标。如果跟踪
hoverinfo包含“文本”标志并且未设置“hovertext”,则这些元素将在悬停标签中看到。
悬停文本:
设置与每个 (x,y) 对关联的悬停文本元素。如果是单个字符串,则相同的字符串出现在所有数据点上。如果是字符串数组,则项目将按顺序映射到该迹线的 (x,y) 坐标。为了被看到,跟踪
hoverinfo必须包含一个“文本”标志。
| 归档时间: |
|
| 查看次数: |
3597 次 |
| 最近记录: |