在 plotly 中格式化 hovertext 或 hoverinfo 信息
Format hovertext or hoverinfo information in plotly
如何格式化我的分组绘图条形图的 hoverinfo()
或 hovertext()
以显示如下数据:
Animals:Giraffe
SF_ZOO:20
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
library(plotly)
fig <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF_ZOO',marker=list(color="#556361"),
hoverinfo = paste(Animals,SF_Zoo))
fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA_ZOO',marker=list(color="#A72608"),
hoverinfo = paste(Animals,SF_Zoo))
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
fig
或者我可以使用 ggplotly()
解决方案。
您可以使用 hovertemplate
:
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
library(plotly)
fig <- plot_ly(
data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF_ZOO',
marker=list(color="#556361"),
hovertemplate = paste("Animals: %{x}" , "<br>SF_Zoo: %{y}", "<extra></extra>")
)
fig <- fig %>% add_trace(
y = ~LA_Zoo, name = 'LA_ZOO',marker=list(color="#A72608"),
hovertemplate = paste("Animals: %{x}" , "<br>LA_Zoo: %{y}", "<extra></extra>")
)
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
fig
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo) %>% pivot_longer(cols = contains("Zoo")) %>%
mutate(label=paste0("<b>", name, "</b><br>", Animals))
gg <- ggplot(data, aes(y=value, x=Animals, fill=name, text=label)) + geom_bar(stat="identity")
ggplotly(gg, tooltip = "text")
如何格式化我的分组绘图条形图的 hoverinfo()
或 hovertext()
以显示如下数据:
Animals:Giraffe
SF_ZOO:20
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
library(plotly)
fig <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF_ZOO',marker=list(color="#556361"),
hoverinfo = paste(Animals,SF_Zoo))
fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA_ZOO',marker=list(color="#A72608"),
hoverinfo = paste(Animals,SF_Zoo))
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
fig
或者我可以使用 ggplotly()
解决方案。
您可以使用 hovertemplate
:
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
library(plotly)
fig <- plot_ly(
data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF_ZOO',
marker=list(color="#556361"),
hovertemplate = paste("Animals: %{x}" , "<br>SF_Zoo: %{y}", "<extra></extra>")
)
fig <- fig %>% add_trace(
y = ~LA_Zoo, name = 'LA_ZOO',marker=list(color="#A72608"),
hovertemplate = paste("Animals: %{x}" , "<br>LA_Zoo: %{y}", "<extra></extra>")
)
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
fig
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo) %>% pivot_longer(cols = contains("Zoo")) %>%
mutate(label=paste0("<b>", name, "</b><br>", Animals))
gg <- ggplot(data, aes(y=value, x=Animals, fill=name, text=label)) + geom_bar(stat="identity")
ggplotly(gg, tooltip = "text")