I'm trying to create a Shiny app to create a scatterplot based on the Iris data set. The code generates the app, but displays only a single point on the graph, no matter what settings I choose in the app. Here's the code:
options(warn = -1)
library(shiny)
library(shinythemes)
library(dplyr)
library(readr)
library(ggplot2)
options(warn=0)
# Define UI
ui <- fluidPage(theme = shinytheme("superhero"),
titlePanel("Iris"),
sidebarLayout(
sidebarPanel(
# Select Inputs
selectInput(inputId = "y",
label = "Y-axis:",
choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
selected = "Sepal.Length"),
selectInput(inputId = "x",
label = "X-axis:",
choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
selected = "Petal.Length")
),
# Output
mainPanel(
plotOutput(outputId = "scatterplot")
)
)
)
# Define server function
server <- function(input, output) {
# Create the scatterplot object the plotOutput function is expecting
output$scatterplot <- renderPlot({
ggplot(data = iris, aes(x = input$x, y = input$y))+
geom_point(aes(color=Species, shape=Species))+
geom_smooth(method="lm")
})
}
shinyApp(ui=ui, server=server)
it's because your input$x is actually a string. So replace aes()
with aes_string()
in your ggplot
call:
library(ggplot2)
# This doesn't work: aes
ggplot(data = iris, aes(x = "Sepal.Length", y = "Sepal.Width"))+
geom_point(aes(color=Species, shape=Species))+
geom_smooth(method="lm")
# This works : aes_string
ggplot(data = iris, aes_string(x = "Sepal.Length", y = "Sepal.Width"))+
geom_point(aes(color=Species, shape=Species))+
geom_smooth(method="lm")
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.