Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中缩放布置的ggplot2_R_Plot_Ggplot2_Shiny_Gridextra - Fatal编程技术网

在R中缩放布置的ggplot2

在R中缩放布置的ggplot2,r,plot,ggplot2,shiny,gridextra,R,Plot,Ggplot2,Shiny,Gridextra,我想根据Shining中的plotOutput()函数中的笔刷范围缩放绘图。这是清楚地说明。但是,我有一个由多个ggplot图组成的图,这些图是用gridExtra包安排的,这里的技巧似乎不再有效了。看起来拉丝的值返回类似网格值的值…但是我不知道如何用原始数据映射拉丝区域以进行缩放。您可以看到问题,代码如下: library(ggplot2) library(Cairo) library(grid) library(gridExtra) ui <- fluidPage( fluid

我想根据Shining中的
plotOutput()
函数中的笔刷范围缩放绘图。这是清楚地说明。但是,我有一个由多个ggplot图组成的图,这些图是用gridExtra包安排的,这里的技巧似乎不再有效了。看起来拉丝的值返回类似网格值的值…但是我不知道如何用原始数据映射拉丝区域以进行缩放。您可以看到问题,代码如下:

library(ggplot2)
library(Cairo) 
library(grid)
library(gridExtra)

ui <- fluidPage(
  fluidRow(
    column(width = 4, class = "well",
           h4("Plot1. Simple ggplot (Brush and double-click to zoom). WORKS!"),
           plotOutput("plot1", height = 300,
                      dblclick = "plot1_dblclick",
                      brush = brushOpts(
                        id = "plot1_brush",
                        resetOnNew = TRUE,direction = "x"
                        ),
                      hover = hoverOpts(id = "plot1_hover",delay = 0)
                      ),
           h5("Plot 1 values hover. Correspond with the data on the plot."),
           verbatimTextOutput(outputId = "values1"),
           h5("Plot 2 values hover. Do not correspond"),
           verbatimTextOutput(outputId = "values2"),
           h5("Plot 3 values hover. When falling in the area, correspond."),
           verbatimTextOutput(outputId = "values3")
           ),
    column(width = 4, class = "well",
           h4("Plot2. Arrange 2 ggplots. (Brush and double-click to zoom). DO NOT WORK.."),
           plotOutput("plot2", height = 600,
                      dblclick = "plot2_dblclick",
                      brush = brushOpts(
                        id = "plot2_brush",
                        resetOnNew = TRUE,direction = "x"
                      ),
                      hover = hoverOpts(id = "plot2_hover",delay = 0)
                      )
           ),
    column(width = 4, class = "well",
           h4("Plot3. Arrange 2 base plots. (Brush and double-click to zoom). PARTIALLY WORK.."),
           plotOutput("plot3", height = 600,
                      dblclick = "plot3_dblclick",
                      brush = brushOpts(
                        id = "plot3_brush",
                        resetOnNew = TRUE,direction = "x"
                      ),
                      hover = hoverOpts(id = "plot3_hover",delay = 0)
           )
    )
  )
)

server <- function(input, output) {

  # Plot1. Single ggplot
  rangesPlots <- reactiveValues(x1 = NULL,x2=NULL,x3=NULL)
  output$plot1 <- renderPlot({
    p <- ggplot(mtcars, aes(carb,mpg)) +
      geom_point() + scale_x_continuous(breaks = seq(0,8,0.25),expand = c(0.015,0))+
      coord_cartesian(xlim = rangesPlots$x1, ylim = rangesPlots$y1) +
      theme(axis.text.x=element_text(angle = 45,hjust = 1))
    p
  })
  observeEvent(input$plot1_dblclick, {
    brush <- input$plot1_brush
    if (!is.null(brush)) {
      rangesPlots$x1 <- c(brush$xmin,brush$xmax)
    } else {
      rangesPlots$x1 <- NULL
    }
  })
  output$values1 <- renderPrint({
    paste0("x=",input$plot1_hover$x," y=",input$plot1_hover$y)
  },width = 100)

  # Plot2. Multiple ggplot2
  output$plot2 <- renderPlot({
    p <- ggplot(mtcars, aes(carb,mpg)) +
      geom_point() + scale_x_continuous(breaks = seq(0,8,0.25),expand = c(0.015,0))+
      coord_cartesian(xlim = rangesPlots$x2) +
      theme(axis.text.x=element_text(angle = 45,hjust = 1))
    p2 <- arrangeGrob(p,p,heights = c(0.4,0.6))
    grid.draw(p2)
  })
  observeEvent(input$plot2_dblclick, {
    brush <- input$plot2_brush
    if (!is.null(brush)) {
      rangesPlots$x2 <- c(brush$xmin,brush$xmax)
    } else {
      rangesPlots$x2 <- NULL
    }
  })
  output$values2 <- renderPrint({
    paste0("x=",input$plot2_hover$x," y=",input$plot2_hover$y)
  },width = 100)

  # Plot3 Base plot
  output$plot3 <- renderPlot({
    par(mfrow=c(2,1))
    plot(mtcars$carb,mtcars$mpg,xaxt="n",xlim=rangesPlots$x3)
    text(x=seq(0,8,0.25)[-c(1:4)],y=par()$usr[3]-0.3,labels = seq(0,8,0.25)[-c(1:4)], srt=45, adj=1, xpd=TRUE)
    plot(mtcars$carb,mtcars$mpg,xaxt="n",xlim=rangesPlots$x3)
    text(x=seq(0,8,0.25)[-c(1:4)],y=par()$usr[3]-0.3,labels = seq(0,8,0.25)[-c(1:4)], srt=45, adj=1, xpd=TRUE)
  })
  observeEvent(input$plot3_dblclick, {
    brush <- input$plot3_brush
    if (!is.null(brush)) {
      rangesPlots$x3 <- c(brush$xmin,brush$xmax)
    } else {
      rangesPlots$x3 <- NULL
    }
  })
  output$values3 <- renderPrint({
    paste0("x=",input$plot3_hover$x," y=",input$plot3_hover$y)
  },width = 100)
}

shinyApp(ui,server)
库(ggplot2)
图书馆(开罗)
图书馆(网格)
图书馆(gridExtra)
用户界面