簡體   English   中英

ggiraph,R:如何將圖例和具有相同 data_id 屬性的 plot 鏈接起來?

[英]ggiraph, R: How to link legend and plot with same data_id attribute?

當我 hover 我的 cursor 在圖表上時,我希望相應的圖例 label 和填充也突出顯示,反之亦然。 為此,他們需要相同的 data_id - 但我正在努力做到這一點。 我如何讓它在我的代碼中工作?

如果我將data_id=groupID groupID 添加到scale_fill_manual_interactive()以使圖例具有交互性,我會收到以下錯誤:

Error in scale_interactive(scale_fill_manual, ...) : 
  object 'groupID' not found

data_id = function(breaks) { as.character(breaks) }有效但它沒有鏈接圖例和 plot。但我找不到解釋為什么它應該有效但 'data_id=groupID' 沒有,所以單獨解決這個問題是不可能的。

這是代碼(編輯我已經設法讓自定義標簽正確顯示並更新了代碼)

library(ggplot2)
library(ggiraph)
library(ggrepel)
library(scales)

Area <- c("location1", "location2", "location3", "location4")
very_good <-  c(14, 7, 17, 16)
good <-  c(33, 31, 35, 31)
quite_bad <-  c(33, 36, 30, 1)
very_bad <-  c(17, 2, 14, 10)

#Custom labels for the legend
Labels <- c("Very good", "Good", "Quite bad", "Very bad, wont return")

df1 <- data.frame(
  Area, 
  very_good, 
  good, 
  quite_bad,
  very_bad
)

df1_subset <- df1 %>%
  mutate_at(vars(2:5), funs(./100)) %>% 
  pivot_longer(
    cols = c(2:5),
    names_to = "Question", values_to = "Result"
  )

df1_subset <- transform(
  df1_subset,groupID=as.numeric(forcats::fct_inorder(Question))
)

set.seed(1)

stacked_chart <- ggplot(
  data = df1_subset,
  aes(
    x = Result,
    y = Area,
    group = Question,
    fill = Question,
    data_id = groupID
  )
) +
  geom_col_interactive(
    position = position_fill(reverse = TRUE)
  ) +
  geom_text_repel_interactive(
    aes(
      color = ifelse(Result > 0.06,  "#FFFFFF", "transparent"),
      label = percent(Result)
    ),
    fontface = "bold",
    position = position_fill(
      reverse = TRUE
    ),
    box.padding = 0.05,
    segment.color = "transparent",
    size = 5,
    direction = "x",
    hjust = 1.5
  ) +
  scale_y_discrete(
    limits = rev(Area)
  ) +
  scale_x_continuous(
    labels = scales::percent,
    expand = c(0, 0),
    limits = c(0, 1)
  ) +
  scale_color_identity() +
  scale_fill_manual_interactive(
data_id = lapply(Labels, function(breaks) {
  as.character(breaks)
}),
labels = function(breaks) {
  lapply(Labels, function(breaks) {
    label_interactive(
      breaks,
      data_id = as.character(breaks)
    )
  })
},
    values = c(
      "#000000",
      "#333333",
      "#666666",
      "#999999"
    )
  ) +
  theme_minimal() +
  theme(
    legend.position = "top",
    legend.justification = "left",
    legend.title = element_blank()
  )

stacked_chart_ggiraph <- girafe(
  ggobj = stacked_chart, width_svg = 9, height_svg = 6,
  options = list(
    opts_sizing(rescale = TRUE),
    opts_toolbar(saveaspng = FALSE),
    opts_hover_inv(css = girafe_css(
      css = "opacity:0.3;"
    )),
    opts_hover(css = girafe_css(
      css = "cursor:pointer;fill:red;",
      text = "cursor:pointer;fill:#222222;"
    )),
    opts_hover_key(css = girafe_css(
      css = "cursor:pointer;fill:red;"
    ))
  )
)

stacked_chart_ggiraph

我最終確實為此找到了解決方案,這本身就是一個奇跡,因為 ggiraph 的文檔仍然非常不完整。 但已經過去幾周了,所以如果我遺漏了什么,請發表評論。

因此 ggiraph 允許使用本文檔中簡要提到的extra_interactive_params參數的自定義功能。

這需要大量的試驗和錯誤,但為了讓它在我的情況下工作,它需要在初始 geom_*_interactive function 中初始化,然后才能使用比例尺 function,我的圖例就是在這里創建的。 我還必須使用反引號``強制 R 接受包含特殊字符(如連字符)的自定義屬性-

以下是相關的代碼:

  geom_col_interactive(
    aes(
      data_id = groupID,
      `data-id` = groupID
    ),
    extra_interactive_params = "data-id",
    position = position_fill(reverse = TRUE)
  ) +

...

  scale_fill_manual_interactive(
    extra_interactive_params = "data-id",
    `data-id` = seq_along(Legend_colours),
    values = Legend_colours,
    labels = function(breaks) {
      lapply(seq_along(Labels), function(breaks) {
        label_interactive(
          Labels[breaks],
          `data-id` = breaks,
          extra_interactive_params = c("data-id")
        )
      })
    }
    ) +

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM