简体   繁体   English

基于缩放交互的子集数据框

[英]Subset dataframe based on zoom interaction

I'm building an interactive shiny app in R and want to display two scatter plots: 我正在R中构建一个交互式的闪亮应用程序,并希望显示两个散点图:

  1. Entire Dataset (left) 整个数据集(左)
  2. Zoomed Region (right) 放大区域(右)

The left plot shouldn't change and is used to select different regions for the right plot. 左图不应更改,用于为右图选择不同的区域。 It is similar to plot2 and plot3 in the example code ( http://shiny.rstudio.com/gallery/plot-interaction-zoom.html ). 它与示例代码( http://shiny.rstudio.com/gallery/plot-interaction-zoom.html )中的plot2plot3相似。

I want to display the statistics and linear regression for both plots and have the information for the zoomed plot get updated based on which region is selected in the right plot. 我想显示两个图的统计信息和线性回归,并根据在正确图中选择的区域来更新缩放图的信息。 One way I thought of doing this was to use the brush to subset the original data (df_mtcars) and save it as a new dataframe (df_mtcars2). 我想到的一种方法是使用笔刷对原始数据(df_mtcars)进行子集并将其保存为新的数据帧(df_mtcars2)。

I'm still a bit new to R and wasn't able to find very much information on this. 我对R还是有点陌生​​,无法找到很多有关此的信息。 I found a method for doing something similar with ggvis ( here ) but is there a way to do it using ggplot2? 我找到了一种与ggvis( here )类似的方法,但是有没有办法使用ggplot2呢? I'm also open to other suggestions in case there is an easier way to do this. 如果有更简便的方法,我也欢迎其他建议。

Here is my code: 这是我的代码:

app.R 应用程序

library(ggplot2)
library(dplyr)

df_mtcars <- mtcars %>%
  select(wt,mpg)

df_mtcars2 <- df_mtcars
#choose selection based on brushed/zoomed data

ui <- fluidPage(
  fluidRow(
    column(width = 12, class = "well",
       h4("Left plot controls right plot"),
       fluidRow(
         column(width = 6,
                h5("Entire Dataset (left)"),
                plotOutput("plot1", height = 350,
                           brush = brushOpts(
                             id = "plot1_brush",
                             resetOnNew = TRUE
                           )
                )
         ),
         column(width = 6,
                h5("Zoomed Region (right)"),
                plotOutput("plot2", height = 350)
         )
       ),
       fluidRow(
         column(width = 6,
                verbatimTextOutput("summary1")),
         column(width = 6, 
                verbatimTextOutput("summary2"))
       )    
    )
  )
)
server <- function(input, output) {

# Linked plots (left and right)
ranges <- reactiveValues(x = NULL, y = NULL)

output$plot1 <- renderPlot({
  ggplot(df_mtcars, aes(wt, mpg)) + geom_point() + 
  geom_smooth(method = "lm", color = "red")
})

output$plot2 <- renderPlot({
  #dataset should be changed to df_mtcars2
  ggplot(df_mtcars2, aes(wt, mpg)) + geom_point() + 
    geom_smooth(method = "lm", color = "blue") +
    # if using df_mtcars2, should get rid of coord_cartesian range (?)
    coord_cartesian(xlim = ranges$x, ylim = ranges$y) 
})

# When a double-click happens, check if there's a brush on the plot.
# If so, zoom to the brush bounds; if not, reset the zoom.
observe({
  brush <- input$plot1_brush
  if (!is.null(brush)) {
    ranges$x <- c(brush$xmin, brush$xmax)
    ranges$y <- c(brush$ymin, brush$ymax)
  } else {
    ranges$x <- NULL
    ranges$y <- NULL
  }
})

output$summary1 <- renderPrint({
  summary(df_mtcars)
  #how to add linear equation and R^2 (?)
})

output$summary2 <- renderPrint({
  summary(df_mtcars2) #should be df_mtcars2
  #how to add linear equation and R^2 (?)
 })
}

To get the brushed data, you can use the brushedPoint function, it outputs the row numbers of the points that are brushed. 要获取刷过的数据,可以使用brushedPoint函数,它输出刷过的点的行号。 You can then pass directly to ggplot in your plot2 and in summary2 . 然后,您可以在plot2ggplot中直接传递给summary2 Here's an example: 这是一个例子:

server <- function(input, output) {

  values <- reactiveValues(data=df_mtcars)

  output$plot1 <- renderPlot({
    ggplot(df_mtcars, aes(wt, mpg)) + geom_point() + 
      geom_smooth(method = "lm", color = "red")
  })

  output$plot2 <- renderPlot({
    ggplot(values$data, aes(wt, mpg)) + geom_point() + 
      geom_smooth(method = "lm", color = "blue") 
  })

  observe({
    if (!is.null(input$plot1_brush)) {      
      values$data <- brushedPoints(df_mtcars, input$plot1_brush)
    } else {     
      values$data <- df_mtcars
    }
  })

  output$summary1 <- renderPrint({
    summary(df_mtcars)
  })

  output$summary2 <- renderPrint({
    summary(values$data)
  })
}

To display the linear equation, you can maybe add a verbatimTextOutput("summary2_lm") in your ui.R , and in your server.R output the linear equation and R2 coefficient: 要显示线性方程,你也许可以添加一个verbatimTextOutput("summary2_lm")在您的ui.R ,并在server.R输出线性方程和R2系数:

output$summary2_lm <- renderPrint({
    m <- lm(mpg ~ wt, values$data);
    paste("y=",format(coef(m)[1], digits = 2),"x+",format(coef(m)[2], digits = 2)," R2=",format(summary(m)$r.squared, digits = 3))
  })

The function to get the equation and R2 as a string is inspired from here 此处获取方程式和R2作为字符串的函数

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

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