当我们将悬停标签移动到R中Plotly图的错误栏上时,有什么方法可以禁用它吗



当我们将悬停标签移动到错误栏上时,有什么方法可以禁用它吗?我正在用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"
)

当我把光标移到条形图上时,我会得到文本值,这很好。但我希望只有当我将光标移动到条形图上时才会显示此信息,并且当我移动到错误条上时不应弹出"无值"。

我愿意接受所有的建议。

只能在geom_col部分定义text变量:

library(ggplot2)
library(plotly)
library(dplyr)
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_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
# ) +
geom_col(position = position_dodge2(width = .9, preserve = "single"), 
aes(text = paste("Product Name:", Product_Name, "<br>", "Cost:", Cost))) +
facet_wrap( ~ key, scales = "free_x", strip.position = "bottom") +
theme(strip.placement = "outside") +
theme_bw()
),
tooltip = "text"
)

编辑:考虑到海报在保持元素排列在(闪亮的?flex?(仪表板中时观察到的问题,如果可以选择的话,在绘图布局中设置autoscale = FALSE可能会有所帮助。也许以下内容适用于绘图部分:


p1 <- ggplot(data = Source_Data, aes(Product_Name, Cost, ymin = Cost - sd,
ymax = Cost + sd, fill = Product_desc)) +
geom_col(position = position_dodge2(width = .9, preserve = "single"), 
aes(text = paste("Product Name:", Product_Name, "<br>", "Cost:", Cost))) +
geom_errorbar(position = position_dodge2(width = .9, preserve = "single", 
padding = .9)) +
facet_wrap( ~ key, scales = "free_x", strip.position = "bottom") +
theme(strip.placement = "outside") +
theme_bw() 
ggplotly(p1, tooltip = "text") %>% plotly::layout(autosize=FALSE)

相关内容

  • 没有找到相关文章

最新更新