简体   繁体   中英

scatter plot in shiny plotting like straight line

I am using selectInput to get the input from the user and the input which Shiny app will get is the column name in my data.frame. Then in the renderPlot, I tried to plot scatter plot by giving x-axis as selected column and y-axis as other fixed column. My data looks like this

在此处输入图像描述

My shiny code:

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

It should plot like this: 在此处输入图像描述

But instead it is plotting like this: 在此处输入图像描述

I have been trying to fix this since last 4 days, but I am not able to. Please help.

You need to replace x=input$ghg in your ggplot code with 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)

Your code takes input$ghg inside aes() as a column itself, not as a string with column name. That's why it is only one value on x axis.

So you need to use either x =.!as.name(input$ghg) or x = !!sym(input$ghg) or x = get(input$ghg) instead inside aes() of ggplot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM