简体   繁体   English

带有ggplotly的shiny仪表板中的响应式plot标题

[英]Responsive plot titles in shiny dashboard with ggplotly

I have a fluid dashboard with lots of graphs created in ggplot and then rendered as ggplotly objects.我有一个流畅的仪表板,其中包含在 ggplot 中创建的大量图形,然后呈现为 ggplotly 对象。 The problem is that on smaller screens or minimised windows, sometimes the plot titles are cut off .问题是在较小的屏幕或最小化的 windows 上,有时 plot 标题会被截断

Is there a way to dynamically wrap plot titles based on screen width, potentially with str_wrap()?有没有办法根据屏幕宽度动态包装 plot 标题,可能使用 str_wrap()?

I've included a reproducible example below:我在下面包含了一个可重现的示例:

library(shiny)
library(tidyverse)
library(plotly)

ui <- fluidPage(
  
  fluidRow(
    column(
      width = 4,
      plotlyOutput("plot1")
    ),
    column(
      width = 4,
      plotlyOutput("plot2")
    ),
    column(
      width = 4,
      plotlyOutput("plot3")
    )
  )
)


server <- function(input, output) {

  output$plot1 <- renderPlotly({
    x <- mtcars %>%
      ggplot(
        aes(
          x = cyl,
          y = hp,
          fill = wt
        )
      ) +
      geom_point() +
      labs(title = "My very, very, very long title number 1")
    
    
    ggplotly(x)
  })
  
  output$plot3 <- renderPlotly({
    x <- mtcars %>%
      ggplot(
        aes(
          x = cyl,
          y = hp,
          fill = wt
        )
      ) +
      geom_point() +
      labs(title = "My very, very, very long title number 2")
    
    
    ggplotly(x)
  })
  
  
  output$plot2 <- renderPlotly({
    x <- mtcars %>%
      ggplot(
        aes(
          x = cyl,
          y = hp,
          fill = wt
        )
      ) +
      geom_point() +
      labs(title = "My very, very, very long title number 3")
    
    
    ggplotly(x)
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

One approach could be using h4 tag instead of plot title, wrapping both h4 and plotlyOutput with column(fluidRow(...) to make a responsive plot title and align the plot and h4 title nicely.一种方法是使用h4标签而不是 plot 标题,用column(fluidRow(...)包装h4plotlyOutput以制作响应式 plot 标题并很好地对齐 Z32FA6E1B78A9D402AA4893 和h4

library(shiny)
library(tidyverse)
library(plotly)

ui <- fluidPage(
  
  fluidRow(
    column(
      width = 4,
      column(
        width = 12,
        fluidRow(
          column(width = 1, HTML("&nbsp;")),
          column(
            width = 11,
            h4("My very, very, very long title number 1")
          )
        ),
        plotlyOutput("plot1")
      )
    ),
    column(
      width = 4,
      column(
        width = 12,
        fluidRow(
          column(width = 1, HTML("&nbsp;")),
          column(
            width = 11,
            h4("My very, very, very long title number 2")
          )
        ),
        plotlyOutput("plot2")
      )
    ),
    column(
      width = 4,
      column(
        width = 12,
        fluidRow(
          column(width = 1, HTML("&nbsp;")),
          column(
            width = 11,
            h4("My very, very, very long title number 3")
          )
        ),
        plotlyOutput("plot3")
      )
    )
  )
)

Then you will have a responsive title, no matter how narrow your screen is.然后,无论您的屏幕有多窄,您都将拥有一个响应式标题。

Server code remains the same.服务器代码保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM