我使用ggplotly绘制图形。当光标在图形顶部移动时,我使用工具提示功能来表示条形图中的值。
Source_Data <-
data.frame(
key = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
Product_Name = c(
"Table",
"Table",
"Chair",
"Table",
"Bed",
"Bed",
"Sofa",
"Chair",
"Sofa"
),
Product_desc = c("XX", "XXXX", "YY", "X", "Z", "ZZZ", "A", "Y", "A"),
sd = c(0.1, 0.3, 0.4, 0.5, 0.6, 0.7, 0.7, 0.8, 0.5),
Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)
)
ggplotly((
Source_Data %>%
ggplot(
aes(
Product_Name,
Cost,
ymin = Cost - sd,
ymax = Cost + sd,
fill = Product_desc,
text = paste("Product Name:", Product_Name, "<br>", "Cost:", Cost)
)
) +
geom_col(position = position_dodge2(width = .9, preserve = "single")) +
geom_errorbar(position = position_dodge2(
width = .9,
preserve = "single",
padding = .5
)) +
geom_text(
aes(y = Cost + sd, label = Cost),
position = position_dodge2(width = .9),
vjust = -1
) +
facet_wrap( ~ key, scales = "free_x", strip.position = "bottom") +
theme(strip.placement = "outside") +
theme_bw()
),
tooltip = "text"
)
当我将光标移到条形上方时,我会得到文本值,这很好。当我将其移动到错误栏的顶部时,我得到的文字值将变为不同的颜色。有没有一种方法可以更改此悬停文本的背景颜色。
我愿意接受所有建议。
您可以%>% layout(hoverlabel=list(bgcolor="white"))
在ggplotly(...)
通话后添加以将悬停标签背景颜色设置为白色。
通常,您可以通过这种方式从https://plot.ly/r/reference/设置任何使用plot_ly()
或创建的图形上的任何Plotly布局属性ggplotly()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句