簡體   English   中英

geom_label不會相應地縮放到Shiny中的繪圖

[英]geom_label doesn't scale correspondingly to plot in Shiny

在構建我的Shiny App時,我遇到了ggplot2圖形在不同窗口大小下看起來非常不同的情況。 第一張圖顯示了完整桌面大小的情節 - 一切都很棒: 在此輸入圖像描述

但是,當我改變輸出窗口的大小時,每個元素似乎都適當縮小,但不是geom_label (參見下圖)。

在此輸入圖像描述

為什么這是一個案例,我如何使geom_label相應縮小?

Shiny的設置是:

ui <- fluidPage(

      mainPanel(
         selectInput('cluster', '', 1:7),
         plotOutput('ap_plot', height = 200)
       )
   )
)

server <- function(input, output, session) {
  output$ap_plot <- renderPlot({
    data %>% 
      filter(cluster == input$cluster) %>% 
      plot_sequences(.by = ts_cluster, .colour = id)
  })
}

shinyApp(ui = ui, server = server)

閃亮文檔中所詳述當前繪圖的繪圖寬度在session$client_data$output_<plotname>_width ,因此對於示例session$client_data$output_ap_plot_width 這可以用於縮放geom_label的text參數。 您沒有提供最小的可重現示例,但這里是一個:

data <- tibble(
  cluster = sample(7, 100, replace = TRUE),
  x = rnorm(100),
  y = rnorm(100)
)

plot_sequences <- function(data_set, width) {
  label_data <- data_set %>% 
    summarise(
      n = n(),
      mean_x = mean(x),
      mean_y = mean(y),
      label = sprintf("N: %d\nMean x: %0.3f\nMean y: %0.3f", n, n, mean_x, mean_y)
    )

  ggplot(data, aes(x, y)) +
    geom_point() +
    geom_label(aes(x = 1.5, y = 1.5, label = label), label_data, size = 4 / 900 * width)
}

ui <- fluidPage(
  mainPanel(
    width = 12,
    selectInput('cluster', '', 1:7),
    plotOutput('ap_plot', height = 200)
  )
)

server <- function(input, output, session) {
  width <- 400
  output$ap_plot <- renderPlot(execOnResize = TRUE, {
    data %>% 
      filter(cluster == input$cluster) %>% 
      plot_sequences(session$clientData[["output_ap_plot_width"]])
  })
}

shinyApp(ui = ui, server = server)

您可以看到我的繪圖功能將繪圖寬度作為輸入,然后相應地縮放文本大小,使用900像素的寬度作為基線。 另請注意,我已將execOnResize設置為TRUE ,否則在重新調整窗口/繪圖大小時,將重放繪圖而不是重新計算。

暫無
暫無

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

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