我有一个流动的面板,里面有很多在ggplot中创建的图,然后渲染为ggplotly对象。问题是,在较小的屏幕或最小化的窗口上,有时情节标题会被切断。
有没有一种方法可以根据屏幕宽度动态包装绘图标题,可能使用str_wrap((?
我在下面包含了一个可复制的例子:
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)
一种方法可以使用h4
标签而不是绘图标题,用column(fluidRow(...)
包装h4
和plotlyOutput
以生成响应的绘图标题,并将绘图和h4
标题很好地对齐。
library(shiny)
library(tidyverse)
library(plotly)
ui <- fluidPage(
fluidRow(
column(
width = 4,
column(
width = 12,
fluidRow(
column(width = 1, HTML(" ")),
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(" ")),
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(" ")),
column(
width = 11,
h4("My very, very, very long title number 3")
)
),
plotlyOutput("plot3")
)
)
)
)
然后你会有一个响应标题,无论你的屏幕有多窄。
服务器代码保持不变。