闪亮的应用程序:无法使用ggplot2生成正确的图

时间:2020-01-08 12:57:33

标签: r ggplot2 shiny

我的问题的解决方案必须是单线的,但我正在使婴儿的脚步更加闪亮。 请在以下示例中查看示例

https://shiny.rstudio.com/gallery/telephones-by-region.html

我已对其进行了修改,以便它使用ggplot2生成图(以及散点图而不是条形图)。我在下面粘贴代码。显然存在一个错误,因为ggplot2不会在y轴上绘制每列的值。谁能帮我吗?谢谢!

 library(tidyverse)
 library(shiny)


 # Rely on the 'WorldPhones' dataset in the datasets
 # package (which generally comes preloaded).
 library(datasets)

 nn <- row.names(WorldPhones) %>% as.numeric

 ww <- WorldPhones %>%
    as_tibble %>%
    mutate(year=nn) %>%
    select(year, everything())

 mycolnames <- colnames(WorldPhones)

 # Use a fluid Bootstrap layout
 ui <- fluidPage(    

     # Give the page a title
     titlePanel("Telephones by region"),

     # Generate a row with a sidebar
     sidebarLayout(      

    # Define the sidebar with one input
     sidebarPanel(
     selectInput("region", "Region:", 
              choices=mycolnames),
      hr(),
      helpText("Data from AT&T (1961) The World's Telephones.")
     ),

     # Create a spot for the barplot
     mainPanel(
      plotOutput("phonePlot")  
    )

    )
    )


 # Define a server for the Shiny app
  server <- function(input, output) {

  # Fill in the spot we created for a plot
   output$phonePlot <- renderPlot({

  ## plot(ww[,input$region]*1000, 
   ##         main=input$region,
   ##         ylab="Number of Telephones",
   ##      xlab="Year")

  ggplot(ww, aes(x=year, y=input$region
                      )) +
  geom_point(size=3, shape=1, stroke=2)  
  })
   }

  shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

问题是您在y=input$region处传递了长度为1的字符向量。您可以这样做以实际传递该列:

ggplot(ww, aes(x=year, y=ww[[input$region]])) +
  geom_point(size=3, shape=1, stroke=2)