我正在尝试创建一个Shiny应用程序,以基于Iris数据集创建散点图。该代码生成了应用程序,但是无论我在应用程序中选择什么设置,该图都仅在图形上显示单个点。这是代码:
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)
答案 0 :(得分:0)
这是因为您输入的$ x实际上是一个字符串。因此,在您的aes()
通话中,将aes_string()
替换为ggplot
:
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")