2017-12-22 76 views
1

저는 사용자로서 여기 새로 왔지만 Rstudio에서 반짝이는 데이터 시각화 응용 프로그램을 만들려고 할 때 발생하는 문제에 대해 미친 듯이 검색했습니다.ggplot2 확대/축소를 사용하는 데 문제가 있습니다.

문제는, .csv를 읽고, 열을 이해하고, x 및 y 축으로 원하는 열을 선택하고 선택한 그래프 유형으로 플롯하고 내가 원할 때마다 보조 음모.

저는 거의 다 왔는데, 제가 시도한 브러시 줌이 제대로 작동하지 않습니다. 그것은 축의 값을 정확하게 이해하지 못합니다. 대신에 축이 0에서 1까지만있는 것처럼 작동 한 다음 잘못된 xlim 및 ylim으로 올바른 방법으로 확대/축소합니다.

library(shiny) 
library(ggplot2) 

base = read.csv("TESTE.csv", sep = ";") 
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área") 

shinyUI(fluidPage(


    titlePanel("MGM"), 


    sidebarLayout(
    sidebarPanel(
     selectInput("selectedColX", "Select colum for X axis", choices = colnames(base), selected = colnames(base)[7]), 
     selectInput("selectedColY", "Select colum for Y axis", choices = colnames(base), selected = colnames(base)[4]), 
     selectInput("selectedColor", "Select colum for colour axis", choices = colnames(base), selected = colnames(base)[6]), 
     selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = tipos[1]) 
    ), 


    fluidRow(

     column(width = 12, class = "well", 
      h4("Left plot controls right plot"), 
      fluidRow(
       column(width = 10, 
         plotOutput("Disp", height = 300, 
           brush = brushOpts(
            id = "Disp_brush", 
            clip = TRUE, 
            resetOnNew = TRUE 
           ) 
        ) 
       ), 
       column(width = 10, 
         plotOutput("DispZoom", height = 300) 
       ) 
      ) 
    ) 

    ) 

# mainPanel(
#  
#  plotOutput("Hist"), 
#  plotOutput("Box"), 
#  plotOutput("Ar") 
# ) 
) 
)) 

그리고 내 Server.R :

library(shiny) 
library(ggplot2) 

base = read.csv("TESTE.csv", sep = ";") 
tipos <- c("Dispersão", "Histograma", "Boxplot", "Área") 

shinyServer(function(input, output) { 

    output$Disp <- renderPlot({ 

    validate(need(input$seletedGraph=="Dispersão", message=FALSE)) 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) 
    gg <- gg + geom_point() 

    plot(gg) 

    }) 

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

    output$DispZoom <- renderPlot({ 

    validate(need(input$seletedGraph=="Dispersão", message=FALSE)) 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y) 
    plot(gg) 

    }) 

    output$Hist <- renderPlot({ 

    validate(need(input$seletedGraph=="Histograma", message=FALSE)) 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis)) 
    gg <- gg + geom_histogram() 
    gg 

    }) 

    output$Box <- renderPlot({ 

    validate(need(input$seletedGraph=="Boxplot", message=FALSE)) 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) 
    gg <- gg + geom_boxplot() 
    gg 

    }) 

    output$Ar <- renderPlot({ 

    validate(need(input$seletedGraph=="Área", message=FALSE)) 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) 
    gg <- gg + geom_area() 
    gg 

    }) 

    observe({ 
    brush <- input$Disp_brush 
    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 
    } 
    }) 

}) 

는 그냥 geom_point 아닌 다른 플롯을 무시

여기 내 ui.R입니다. 이 작업을하면 다른 사람들도 잘 작동 할 것입니다. 내 생각에 ...

고마워요, 이걸 알아 내려고 고맙습니다. 일부 텍스트는 포르투갈어로되어 있지만, 모든 것이 충분히 이해할 수 있다고 생각합니다.

답변

0

변수를 print 또는 plot 변수를 반환하는 대신 brushOpts에서 0부터 1까지의 눈금을 표시합니다.

1. 짧은 desmonstration

이 짧은 응용 프로그램은 닦았 점 사이의 차이가 반환 된 방법에 따라 확장을 보여줍니다. 귀하의 질문에서

library(shiny) 


ui <- fluidPage(

    fluidRow(
    column(6, 
     # My plot rendering with print or plot 
     h4("Plot with print or plot variable"), 
     plotOutput("plot1", height = 300, brush = brushOpts(id = "plot1_brush", clip = TRUE, resetOnNew = TRUE)), 
     p(), 
     # Brushed points 
     "Brushed points informations, scale from 0 to 1", 
     verbatimTextOutput("brush1") 
    ), 
    column(6, 
     # My plot rendering without print or plot 
     h4("Plot with a return variable"), 
     plotOutput("plot2", height = 300, brush = brushOpts(id = "plot2_brush", clip = TRUE, resetOnNew = TRUE)), 
     p(), 
     # Brushed points 
     "Brushed points informations, scale according to x and y variables", 
     verbatimTextOutput("brush2") 
    ) 
) 
) 


server <- function(input, output) { 

    data <- iris 

    # Plot1 I render with print or plot 
    output$plot1 <- renderPlot({ 
    gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point() 
    plot(gg) 
    }) 

    # Brush points from plot1 
    output$brush1 <- renderPrint({ 
    input$plot1_brush 
    }) 

    # Plot2 I render just returning the variable 
    output$plot2 <- renderPlot({ 
    gg <- ggplot(data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + geom_point() 
    return(gg) 
    }) 

    # Brush points from plot2 
    output$brush2 <- renderPrint({ 
    input$plot2_brush 
    }) 
} 


shinyApp(ui = ui, server = server) 

2 Reproductible 예는

은 내가 iris 데이터 집합을 사용하여 reproductible 예를했다 Herebelow.
또한 악센트 때문에 일부 문자를 변경했습니다. 답장을

는 ui.R

library(shiny) 
library(ggplot2) 

shinyUI(fluidPage(


    titlePanel("MGM"), 


    sidebarLayout(
    sidebarPanel(
     uiOutput("plots_parameters") 
    ), 

    mainPanel(
     fluidRow(
     column(12, 
      h4("Plot without zoom"), 
      plotOutput("Disp", height = 300, brush = brushOpts(id = "Disp_brush", clip = TRUE, resetOnNew = TRUE)) 
     ) 
    ), 
     fluidRow(
     column(12, 
      h4("Zoomed plot"), 
      plotOutput("DispZoom", height = 300) 
     ) 
    ) 
    ) 
) 
)) 

server.R

library(shiny) 
library(ggplot2) 

base = iris 


shinyServer(function(input, output) { 

    output$plots_parameters <- renderUI({ 
    tipos <- c("Dispersao", "Histograma", "Boxplot", "Área") 
    choices <- colnames(base) 
    div(
     selectInput("selectedColX", "Select colum for X axis", choices = choices, selected = "Sepal.Length"), 
     selectInput("selectedColY", "Select colum for Y axis", choices = choices, selected = "Petal.Length"), 
     selectInput("selectedColor", "Select colum for colour axis", choices = choices, selected = "Species"), 
     selectInput("seletedGraph", "Select type of graph", choices = tipos, selected = "Dispersao") 
    ) 
    }) 

    output$Disp <- renderPlot({ 

    req(input$seletedGraph == "Dispersao") 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) 
    gg <- gg + geom_point() 

    # Return variable without print or plot 
    gg 

    }) 

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

    output$DispZoom <- renderPlot({ 

    req(input$seletedGraph == "Dispersao") 

    y_axis <- input$selectedColY 
    x_axis <- input$selectedColX 
    color_axis <- input$selectedColor 

    gg <- ggplot(base, aes_string(x = x_axis, y = y_axis, color = color_axis)) + geom_point() + 
     coord_cartesian(xlim = ranges2$x, ylim = ranges2$y) 
    # Return variable without print or plot 
    gg 

    }) 

    observe({ 
    brush <- input$Disp_brush 
    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 
    } 
    }) 

}) 
+0

감사합니다,하지만 난이 추가 시도하고 아직도 난 ... 작동하지 않습니다 대신 브러시를 사용하고 확대하는 것이 생각되면 아마도 x 및 y 크기의 슬라이더가있는 것이 더 쉽습니다. 어떻게 나를 위해 일하지 않는지 보여주기 위해 그림을 여기에 추가하는 방법을 모르겠습니다. P –

+0

맞습니다! 재현 가능한 솔루션으로 내 대답을 업데이트했습니다. – qfazille

+0

정말 고마워요! 나는 지금 완벽하게 일했다 : D –