簡體   English   中英

將 plot 散布在 shiny 中,像直線一樣繪制

[英]scatter plot in shiny plotting like straight line

我正在使用 selectInput 從用戶那里獲取輸入,而 Shiny 應用程序將獲得的輸入是我的 data.frame 中的列名。 然后在 renderPlot 中,我嘗試通過將 x 軸作為選定列並將 y 軸作為其他固定列來分散 plot。 我的數據看起來像這樣

在此處輸入圖像描述

我的 shiny 代碼:

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),
selectInput(inputId = "ghg", label = "Choose a Coral type",
            c("CO2" = "CO2_emissions",
              "Methane" = "methane_emissions",
              "Nitrous Oxide" = "nitrous_oxide",
              "Other" = "HFC_PFC_SF6")),

    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )

)

# Define server logic required to draw a histogram
server <- function(input, output) {

output$distPlot <- renderPlot({

    climate_change <- read_excel('climate_change_global.xlsx')

    gg <- ggplot(climate_change, aes(x=input$ghg, y=temp)) + geom_point() + 
        geom_smooth(method="lm") + ggtitle("CO2 corelation with temperature is 91%")
    gg

 })
 }

 # Run the application 
 shinyApp(ui = ui, server = server)

它應該像這樣 plot : 在此處輸入圖像描述

但相反,它是這樣繪制的: 在此處輸入圖像描述

自過去 4 天以來,我一直在嘗試解決此問題,但我無法解決。 請幫忙。

您需要將ggplot代碼中的x=input$ghg替換為x=get(input$ghg)

library(shiny)
library(ggplot2)

dat <- structure(list(year = 1970:1981, flood = c(31L, 15L, 15L, 20L, 
                                                  19L, 17L, 17L, 48L, 47L, 34L, 39L, 43L), CO2_emissions = c(14788798L, 
                                                                                                             15323176L, 15957193L, 16822109L, 16850822L, 16745792L, 17726098L, 
                                                                                                             18279804L, 18497906L, 19533548L, 19324327L, 18726247L), methane_emissions = c(5305820L, 
                                                                                                                                                                                           5145430L, 5360610L, 5421500L, 5379140L, 5503270L, 5649430L, 5796380L, 
                                                                                                                                                                                           5795950L, 5972760L, 6011410L, 5871770L), nitrous.oxide = c(2225930L, 
                                                                                                                                                                                                                                                      2089239L, 2245411L, 2291021L, 2240345L, 2329579L, 2412556L, 2524966L, 
                                                                                                                                                                                                                                                      2552326L, 2710504L, 2732926L, 2636113L), HFC_PFC_SF6 = c(4516068L, 
                                                                                                                                                                                                                                                                                                               3266672L, 3999660L, 3880678L, 3357342L, 3642854L, 3876927L, 4303459L, 
                                                                                                                                                                                                                                                                                                               4341088L, 4947684L, 5039591L, 4554856L), temp = c(0.0372, -0.0783, 
                                                                                                                                                                                                                                                                                                                                                                 0.0264, 0.1641, -0.0719, 0.0034, -0.0792, 0.1978, 0.1123, 0.2273, 
                                                                                                                                                                                                                                                                                                                                                                 0.2637, 0.2999), glacier_melt = c(-10.128, -10.288, -10.441, 
                                                                                                                                                                                                                                                                                                                                                                                                   -10.538, -10.613, -10.534, -10.633, -10.682, -10.754, -11.127, 
                                                                                                                                                                                                                                                                                                                                                                                                   -11.318, -11.394)), class = "data.frame", row.names = c(NA, -12L
                                                                                                                                                                                                                                                                                                                                                                                                   ))

ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),
    selectInput(inputId = "ghg", label = "Choose a Coral type",
                c("CO2" = "CO2_emissions",
                  "Methane" = "methane_emissions",
                  "Nitrous Oxide" = "nitrous_oxide",
                  "Other" = "HFC_PFC_SF6")),

    # Show a plot of the generated distribution
    mainPanel(
        plotOutput("distPlot")
    )

)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({

        climate_change <- dat #read_excel('climate_change_global.xlsx')

        gg <- ggplot(climate_change, aes(x=get(input$ghg), y=temp)) + geom_point() + 
            geom_smooth(method="lm") + ggtitle("CO2 corelation with temperature is 91%")+
            xlab(input$ghg)
        gg

    })
}

# Run the application 
shinyApp(ui = ui, server = server)

您的代碼將aes()中的input$ghg作為列本身,而不是作為具有列名的字符串。 這就是為什么它在 x 軸上只有一個值。

因此,您需要在 ggplot 的aes()中使用x =.!as.name(input$ghg) ggplot x = !!sym(input$ghg)x = get(input$ghg)

暫無
暫無

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

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