Y 轴标签宽度打破了闪亮应用程序中两个图之间的对齐方式

Y axis labels width breaks the alignment between two plots in shiny app

我有下面这个闪亮的应用程序,我希望两个图根据日期完美对齐。问题是,当我的 dummy$value = runif(365) + seq(-140, 224)^2 / 10000 所以我得到的值不长时,对齐有效,但是如果例如 value = runif(365) + seq(-140, 224)^2 / 10 这是我的实际值,那么 value 在 y 轴上包含更长的数字。也许将 6000 改为 6k 会解决问题。

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
# Dummy data
data <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) + seq(-140, 224)^2 / 10
)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    column(width = 8,
           fluidRow(
             shinydashboard::box(width = 12,  plotlyOutput(outputId = "detailed_plot_2", height = "600px" ))
           ),
           fluidRow(
             shinydashboard::box(width = 12,  plotlyOutput(outputId = "detailed_plot_22", height = "100px" ))
           )
           
    )
  )
)

server <- function(input, output) {
  output$detailed_plot_2<-renderPlotly({
    # Most basic bubble plot
    
    p <- ggplot(data, aes(x=day, y=value)) +
      geom_line() + 
      xlab("")
    ggplotly(p)
  })
  output$detailed_plot_22<-renderPlotly({
    plot <-data %>%
      ggplot(aes(day,value)) +
      geom_col() +
      labs(y = "", x = "") +
      theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.line = element_line(colour = 'black'))
    
    ggplotly(plot)
  })
}

shinyApp(ui, server)    

我建议使用 subplotshareX = TRUE 对齐 x 轴。 这也使轴在用户缩放时保持同步:

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)

# Dummy data
data <- data.frame(
  day = as.Date("2017-06-14") - 0:364,
  value = runif(365) + seq(-140, 224)^2 / 10
)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    column(width = 8,
           fluidRow(
             shinydashboard::box(width = 12,  plotlyOutput(outputId = "detailed_plot_2", height = "800px" ))
           )
           
    )
  )
)

server <- function(input, output) {
  output$detailed_plot_2 <- renderPlotly({
    # Most basic bubble plot
    p_2 <- ggplot(data, aes(x=day, y=value)) +
      geom_line() + 
      xlab("")
    
    p_22 <- data %>%
      ggplot(aes(day,value)) +
      geom_col() +
      labs(y = "", x = "") +
      theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.line = element_line(colour = 'black'))
    
    subplot(ggplotly(p_2), ggplotly(p_22), nrows = 2, heights = c(0.84, 0.16), shareX = TRUE)
  })
  
}

shinyApp(ui, server)