【问题标题】:rshiny plotting multiple graphs using data framer闪亮使用数据框绘制多个图
【发布时间】:2017-01-17 17:56:03
【问题描述】:

我已经使用一个数据集构建了多个模型。模型构建后,我创建了错误并生成了一个表作为休耕:

    Model1   Model2        Model3        Model4        Model5 TestData     time
1  636.854 578.691       623.360       638.430      691.028   5757 Oct 2015
2  566.647 627.868       562.838       590.698      563.355   512 Nov 2016
3  569.796 568.839       572.238       573.294      568.711   568 Dec 2015
4  567.660 561.306       576.710       573.889      564.812   534 Jan 2016
5  5653.244 514.589       575.057       522.061      514.812   434 Feb 2016
6  593.142 533.017       601.882       616.739      610.158   540 Mar 2016
7  594.040 596.805       607.002       609.083      619.537   526 Apr 2016

我想把它带入一个 rshiny 应用程序,其中 y 轴是每个模型的值,x 轴是时间。基本上是一个包含所有线条的情节。

如果我在 ggplot 中做:

plt <- ggplot(data=molted,
              aes(x=time, y=value, colour=variable, group=variable)) + geom_line(size=1.5) + 
  xlab("Date") + ylab("Values") + theme(axis.text.y  = element_text(size=20),
                                                           axis.title.y  = element_text(size=28),
                                                           axis.text.x  = element_text(size=20, angle=45, hjust=1),
                                                           axis.title.x  = element_text(size=26))

我从非常基础的开始:

library(shiny)
shinyServer(
  function(input, output) {
  }
)
shinyUI(fluidPage(
  titlePanel(title="This is the first shiny app, hello"),
  sidebarLayout(position = "right",
    sidebarPanel(h3("this is the side bar panel"), h4("widget4"), h5("widget5")),
    mainPanel(h4(" this is the main panel, out is displayed here"),
              h5(" this is the output5"))
  )
))

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    您可以尝试以下方法开始(然后考虑将time 转换为Date 类并使用scale_x_date 等)

    df <- read.table(text=' Model1   Model2        Model3        Model4        Model5 TestData     time
                     1  636.854 578.691       623.360       638.430      691.028   5757 Oct2015
                     2  566.647 627.868       562.838       590.698      563.355   512 Nov2016
                     3  569.796 568.839       572.238       573.294      568.711   568 Dec2015
                     4  567.660 561.306       576.710       573.889      564.812   534 Jan2016
                     5  5653.244 514.589       575.057       522.061      514.812   434 Feb2016
                     6  593.142 533.017       601.882       616.739      610.158   540 Mar2016
                     7  594.040 596.805       607.002       609.083      619.537   526 Apr2016', header=TRUE)
    library(reshape2)
    melted <- melt(df, id='time')
    
    library(shiny)
    server <- shinyServer(
      function(input, output) {
        output$plt<-renderPlot({
          ggplot(data=molted,
                 aes(x=time, y=value, colour=variable, group=variable)) + geom_line(size=1.5) + 
                  xlab("Date") + ylab("Values") + theme(axis.text.y  = element_text(size=20),
                                                  axis.title.y  = element_text(size=28),
                                                  axis.text.x  = element_text(size=20, angle=45, hjust=1),
                                                  axis.title.x  = element_text(size=26))},height = 400,width = 600)
      }
    )
    ui <- shinyUI(fluidPage(
      titlePanel(title="This is the first shiny app, hello"),
      sidebarLayout(position = "right",
                    sidebarPanel(h3("this is the side bar panel"), h4("widget4"), h5("widget5")),
                    mainPanel(plotOutput("plt"))
      )
    ))
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 1970-01-01
      • 2021-06-21
      • 2019-06-15
      • 2019-05-20
      • 2017-03-07
      • 2017-01-23
      • 2020-12-21
      • 2018-09-26
      相关资源
      最近更新 更多