繁体   English   中英

ShinyApp散点图仅显示一个点

[英]ShinyApp scatterplot displays only one point

我正在尝试创建一个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)

这是因为您输入的$ x实际上是一个字符串。 因此,在ggplot调用aes_string() aes()替换为aes_string()

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

请参阅: 将字符串传递给ggplot函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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