简体   繁体   English

使用Shiny和ggplot2在单个图形上绘制多条线

[英]Plotting multiple lines on a single graph using Shiny and ggplot2

I'm trying to build an app that will let the user plot multiple lines on a single graph. 我正在尝试构建一个应用程序,让用户在一个图表上绘制多行。 My data contains tourism data on multiple country by years. 我的数据包含多个国家/地区的旅游数据。

> head(tourism)
  years Bulgaria Czech Republic   Denmark   Poland   Sweden   Norway
1  1995 5437.963       10274.98  9651.070 5523.500 7860.659 7294.971
2  1996 5921.961       13640.53 10810.187 5594.191 7716.620 7236.490
3  1997 5476.931       14932.49 10918.596 7579.637 7658.900 7243.111
4  1998 5197.050       16218.00 10287.564 7229.771 8029.087 7868.735
5  1999 4382.405       16125.00  9965.684 5644.924 8600.785 7814.983
6  2000 5170.091       15597.09 10005.887 6891.283 8654.086 7468.899

Now I'm trying to plot the data with the 'years' column as the x-axis and the other columns as the y-axis. 现在我正在尝试绘制数据,其中'years'列为x轴,其他列为y轴。

ui = fluidPage(
  titlePanel("Tourism"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("cnt",
                  "Select Country:",
                  choices = c("Bulgaria", 
                              "Czech Republic",
                              "Denmark",
                              "Poland",
                              "Sweden",
                              "Norway"),
                  selected = "Bulgaria",
                  multiple = TRUE
    )
    ),
    mainPanel(
      plotOutput("plot")
    )
  ) 
)


server = function(input, output) {

  output$plot = renderPlot({
    ggplot(tourism) +
      geom_line(mapping = aes(x = years, y = tourism[,input$cnt], colour = input$cnt)) + 
      labs (x = "Years", y = "Nights spent per 1000", title = "Tourism") + 
      scale_colour_discrete(name = "Country")
  })

}

shinyApp(ui = ui, server)

The problem is, if I try to plot more than one country at a time, I get the following error: "Aesthetics must be either length 1 or the same as the data (21): x, y, colour". 问题是,如果我尝试一次绘制多个国家,我会收到以下错误:“美学必须是长度1或与数据(21)相同:x,y,颜色”。

While searching, I figured that I'm missing an observe or reactive part, but I just cant figure out how to add it. 在搜索时,我发现我错过了观察或反应部分,但我无法弄清楚如何添加它。

Any help would be appreciated. 任何帮助,将不胜感激。

Melt your data set to long form and then subset to plot. 将您的数据集融合为长格式,然后将子集融合到绘图中。 Try the following (no sample data provided so I'm pretty sure this will work but not tested): 尝试以下(没有提供样本数据,所以我很确定这将工作,但没有测试):

#when loading server.R
library(reshape2)

And then: 接着:

 output$plot = renderPlot({
    plot.data <- melt(tourism, id.vars = 'years')
    #not sure if input$cnt is a list or a vector
    #may need to manipulate that before passing
    plot.data <- plot.data[plot.data$variable %in% input$cnt, ]
    ggplot(plot.data) +
      geom_line(mapping = aes(x = years, y = value, colour = variable)) + 
      labs (x = "Years", y = "Nights spent per 1000", title = "Tourism") + 
      scale_colour_discrete(name = "Country")
  })

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

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