首页 > 解决方案 > 像直线一样闪亮的散点图

问题描述

我正在使用 selectInput 从用户那里获取输入,而 Shiny 应用程序将获得的输入是我的 data.frame 中的列名。然后在渲染图中,我尝试通过将 x 轴作为选定列并将 y 轴作为其他固定列来绘制散点图。我的数据看起来像这样

在此处输入图像描述

我闪亮的代码:

# 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)

它应该像这样绘制: 在此处输入图像描述

但相反,它是这样绘制的: 在此处输入图像描述

自过去 4 天以来,我一直在尝试解决此问题,但我无法解决。请帮忙。

标签: rshinyscatter-plotshinyappsshiny-reactivity

解决方案


您的代码将input$ghg内部aes()作为列本身,而不是作为具有列名的字符串。这就是为什么它在 x 轴上只有一个值。

因此x = !!as.name(input$ghg)x = !!sym(input$ghg)您需要x = get(input$ghg)aes().ggplot


推荐阅读