RのPlotlyグラフでエラーバーの上に移動したときにホバーラベルを無効にする方法はありますか?

2020-03-30 r plotly shinydashboard ggplotly errorbar

エラーバーの上に移動したときにホバーラベルを無効にする方法はありますか?私は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"
   )

バーの上にカーソルを移動すると、テキスト値が表示されます。しかし、バーの上にカーソルを移動したときにのみ表示され、エラーバーの上に移動すると値がポップアップ表示されなくなります。

すべての提案を受け付けています。

Answers

text変数はgeom_col部分でのみ定義できます。

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"
)

編集: (光沢のある?フレックス?)ダッシュボードに要素を並べたままにするというポスターの観察された問題を考えると、もしそれがオプションであるなら、プロットレイアウトで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) 
 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) 
 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) 
 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) 
 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) 
 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) 
 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) 
 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) 
 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) 
 
 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) 
 

Related