簡體   English   中英

在ggplot2圖形上以閃亮形式輸入相關結果

[英]Inputting results of a correlation on ggplot2 figure in shiny

我正在嘗試在閃亮的應用程序內的ggplot2圖形上顯示相關結果。 我在此示例中使用虹膜數據集。 具體來說,我希望xy輸入調用兩個變量的相關性。 而且我可以弄清楚我在做什么錯。 我不斷收到錯誤:

警告:cor.test.default中的錯誤:有限的觀測值不足

但是,我知道當我嘗試評估閃亮的應用程序之外的關系時並非如此:

cor.test(iris$Sepal.Width, iris$Petal.Width, alternative = "two.sided", method="spearman")

我將cor.test調用包裝在一個reactive函數中,並嘗試使用geom_text顯示一個元素。 以下是我的ui.R和server.R代碼。

有人可以在這里發現我做錯了嗎?

用戶界面

library(shiny)
library(ggplot2)
library(dplyr)


dataset <- iris

shinyUI(pageWithSidebar(

  headerPanel("Iris Data Explore"),

  sidebarPanel(

    selectInput('x', 'X', names(dataset), names(dataset)[[2]]),
    selectInput('y', 'Y', names(dataset), names(dataset)[[4]]),
    selectInput('species', 'Species', levels(dataset$Species), "virginica"),
    selectInput('color', 'Color', c('None', names(dataset))),

    checkboxInput('smooth', 'Smooth'),
    checkboxInput('line', 'Line')
  ),

  mainPanel(
    plotOutput('plot')
  )
))

服務器

library(shiny)
library(ggplot2)
library(dplyr)


shinyServer(function(input, output) {

  dataset <- reactive({
    filter(iris,Species==input$species)
  })

  cordf <- reactive({
    cor.test(as.numeric(input$x), as.numeric(input$y), alternative = "two.sided", method="spearman")
  })

  output$plot <- renderPlot({

    p <- ggplot(dataset(), aes_string(x=paste0("`",input$x,"`"),
                                      y=paste0("`",input$y,"`")
                                      )) + 
      geom_point() +
      geom_text(data=cordf(), aes(x=mean(input$x), y=mean(input$y), label=paste0(cordf$estimate))) +
      ggtitle(paste0(input$species))

    if (input$color != 'None')
      p <- p + aes_string(color=input$color)


    if (input$smooth)
      p <- p + geom_smooth(aes(group=1))
    if (input$line)
      p <- p + geom_line(aes(group=1), colour='seagreen')

    print(p)

  }, height=500)

})

問題是dataset甚至沒有在函數cordf()定義,因此出現錯誤“有限的觀察不足”。

另一個問題是您不能僅僅執行mean(input$x)因為input$x只是Sepal.Length字符串。 我也認為您想要annotate而不是geom_text

修改后的server.R在下面。 我不熟悉閃亮效果,因此不確定是否最好的做法。

服務器

library(shiny)
library(ggplot2)
library(dplyr)


shinyServer(function(input, output) {

  dataset <- reactive({
    filter(iris,Species==input$species)
  })

  cordf <- reactive({
    dataset<-dataset();
    cor.test(as.numeric(dataset[,input$x]), as.numeric(dataset[,input$y]), alternative = "two.sided", method="spearman")
  })

  output$plot <- renderPlot({
    dataset<-dataset()
    p <- ggplot(dataset, aes_string(x=paste0("`",input$x,"`"),
                                      y=paste0("`",input$y,"`")
                                      )) +
      geom_point() +
      annotate("text", x=mean(dataset[,input$x]),y=mean(dataset[,input$y]), label=cordf()$estimate) +
      ggtitle(paste0(input$species))

    if (input$color != 'None')
      p <- p + aes_string(color=input$color)


    if (input$smooth)
      p <- p + geom_smooth(aes(group=1))
    if (input$line)
      p <- p + geom_line(aes(group=1), colour='seagreen')

    print(p)

  }, height=500)

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM