为什么我的缩放图从 ggplot2 空白处闪亮

Why is my zoom plot in shiny from ggplot2 blank

我有一个在 ggplot2 中创建的时间序列图,将转到 shinydashbaord 中的一个面板。我需要底部面板作为时间序列图的缩放图。出于某种原因,我的缩放图不显示时间序列图中的数据或轴值。非常感谢这里的任何帮助。

下面的示例使用了在边栏中设置的日期范围。如果在此示例中更改了日期范围,则绘图将不起作用。我的实际工作对日期没有那么敏感,这只是为了演示。

library (shiny)
library (shinydashboard)
library (ggplot2)

header <- dashboardHeader(title = "Example")

sidebar <- dashboardSidebar(
 sidebarMenu(
   menuItem("Data Estimation", tabName = "tabset1",
      dateRangeInput("dates", 
                    "Date range for regression",
                    start = as.character(Sys.Date() - 495), 
                    end = as.character(Sys.Date() - 396))
)))

body <- dashboardBody(
 fluidRow(
  tabBox(id = "tabset1", height = "450px", width = "900px",
   tabPanel("Time Series Est", 
     plotOutput ("plot1", height = 400,
                 brush = brushOpts(
                   id = "plot1Brush",
                   resetOnNew = TRUE)))
)),
 fluidRow(
  tabBox(id = "tabset1", height = "450px", width = "900px",
   tabPanel("Zoom", 
     plotOutput ("plot2", height = 400)
))))

ui <- dashboardPage(skin = "blue", header, sidebar, body)

server <- function(input, output) ({

output$plot1 <- renderPlot({

dat <- data.frame(x = seq(1,100,1), y = seq(1,2000,20), z = seq(1,3000,30), dateTime = seq(input$dates[1], input$dates[2], 1))

p <- ggplot() +
 geom_line(data = dat, aes(x = dateTime, y = x, color = "x")) +
 geom_line(data = dat, aes(x = dateTime, y = y, color = "y")) +
 geom_line(data = dat, aes(x = dateTime, y = z, color = "z")) +
 scale_y_log10() +
 theme_bw()

print(p)

})

ranges2 <- reactiveValues(x = NULL, y = NULL)

observe({
 brush <- input$plot1Brush
 if (!is.null(brush)) {
  ranges2$x <- c(brush$xmin, brush$xmax)
  ranges2$y <- c(brush$ymin, brush$ymax)
}
else {
 ranges2$x <- NULL
 ranges2$y <- NULL
}
})


output$plot2 <- renderPlot({

if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}

dat <- data.frame(x = seq(1,100,1), y = seq(1,2000,20), z = seq(1,3000,30), dateTime = seq(input$dates[1], input$dates[2], 1))

p <- ggplot() +
 geom_line(data = dat, aes(x = dateTime, y = x, color = "x")) +
 geom_line(data = dat, aes(x = dateTime, y = y, color = "y")) +
 geom_line(data = dat, aes(x = dateTime, y = z, color = "z")) +
 coord_cartesian(xlim = ranges2$x, ylim = ranges2$y) +
 scale_y_log10() +
 theme_bw()

print(p)

})

})

shinyApp(ui, server)

你的数据序列向量长度不同。

将您的数据框定义(两者)替换为:

n <- 100
dat <- data.frame(x = seq(1,to=100,length.out=n), 
                  y = seq(1,to=2000,length.out=n), 
                  z = seq(1,to=3000,length.out=n), 
                  dateTime = seq(input$dates[1], to=input$dates[2],length.out=n)) 

我对此进行了测试,效果很好。在我更改日期并且绘图为空白时出错并发出空白图之前。我认为哪个是问题所在?

完整代码如下:

library (shiny)
library (shinydashboard)
library (ggplot2)

header <- dashboardHeader(title = "Example")

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Data Estimation", tabName = "tabset1",
             dateRangeInput("dates", 
                            "Date range for regression",
                            start = as.character(Sys.Date() - 495), 
                            end = as.character(Sys.Date() - 396))
    )))

body <- dashboardBody(
  fluidRow(
    tabBox(id = "tabset1", height = "450px", width = "900px",
           tabPanel("Time Series Est", 
                    plotOutput ("plot1", height = 400,
                                brush = brushOpts(
                                  id = "plot1Brush",
                                  resetOnNew = TRUE)))
    )),
  fluidRow(
    tabBox(id = "tabset1", height = "450px", width = "900px",
           tabPanel("Zoom", 
                    plotOutput ("plot2", height = 400)
           ))))

ui <- dashboardPage(skin = "blue", header, sidebar, body)

server <- function(input, output) ({

  output$plot1 <- renderPlot({

   # dat <- data.frame(x = seq(1,100,1), y = seq(1,2000,20), z = seq(1,3000,30), dateTime = seq(input$dates[1], input$dates[2], 1))
    n <- 100
    dat <- data.frame(x = seq(1,to=100,length.out=n), 
                      y = seq(1,to=2000,length.out=n), 
                      z = seq(1,to=3000,length.out=n), 
                      dateTime = seq(input$dates[1], to=input$dates[2],length.out=n))   
    p <- ggplot() +
      geom_line(data = dat, aes(x = dateTime, y = x, color = "x")) +
      geom_line(data = dat, aes(x = dateTime, y = y, color = "y")) +
      geom_line(data = dat, aes(x = dateTime, y = z, color = "z")) +
      scale_y_log10() +
      theme_bw()
    p

  })

  ranges2 <- reactiveValues(x = NULL, y = NULL)

  observe({
    brush <- input$plot1Brush
    if (!is.null(brush)) {
      ranges2$x <- c(brush$xmin, brush$xmax)
      ranges2$y <- c(brush$ymin, brush$ymax)
    }
    else {
      ranges2$x <- NULL
      ranges2$y <- NULL
    }
  })


  output$plot2 <- renderPlot({

    if (!is.null(ranges2$x)) {
      ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
    }
    n <- 100
    dat <- data.frame(x = seq(1,to=100,length.out=n), 
                      y = seq(1,to=2000,length.out=n), 
                      z = seq(1,to=3000,length.out=n), 
                      dateTime = seq(input$dates[1], to=input$dates[2],length.out=n))

    p <- ggplot() +
      geom_line(data = dat, aes(x = dateTime, y = x, color = "x")) +
      geom_line(data = dat, aes(x = dateTime, y = y, color = "y")) +
      geom_line(data = dat, aes(x = dateTime, y = z, color = "z")) +
      coord_cartesian(xlim = ranges2$x, ylim = ranges2$y) +
      scale_y_log10() +
      theme_bw()

    p

  })

})

shinyApp(ui, server)

在您的 renderPlot() 表达式中,不使用 print(p),而是替换为 p

对我有用。