如何以mm-yyyy格式按时间顺序排序ggplot2 x轴日期?

时间:2013-07-26 01:43:26

标签: r ggplot2 shiny

我正在设计一个R程序来输出任何csv文件输入的不同图形。我正在使用Rstudio Shiny和ggplot2开发程序。

我的问题涉及按时间顺序而不是按字母顺序排序日期(显然是默认的)。我们以此代码为例(我的代码有点不同,但这是来自早些时候帮助过我的人的代码):

相关帖子: Unable to change the graph form of my ggplot rshiny program, help me find the bug?

Sorting months in R How do you order a nominale variable. e.g month in R?

Boxplot with ggplot2 in R - returns by month

server.R

library(shiny)
library(datasets)
library(ggplot2)

X <- read.csv(file.choose())
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot",
                names(Y()) 
                )
  })

  output$opt.y <- renderUI({
    selectInput("ycolumn", "Y Column",
                names(Y()))
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- X
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(X, n = input$obs)
  })

  createPlot <- function(df, colx, coly) {
    p <- ggplot(data=df, aes(x=df[,colx],y=df[,coly]), environment = environment()) #+ geom_line(aes(x=df[,colx],y=df[,coly], group=colx))
    p <- p + geom_line(aes(group=colx)) 
    p <- p + xlab(names(df)[colx]) + ylab(names(df)[coly]) 
  }

  Y <- reactive({
    X
      })



  # create a basic plot
  output$plotBasic <- reactivePlot(function() {
    df <- Y()
    print(createPlot(df, colx=input$xcolumn, coly=input$ycolumn))

  })
})

ui.R

library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
  # Application title
  headerPanel("My app!"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    numericInput("obs", "Number of observations to view:", 13),
    uiOutput("opt.x"), #dynamic UI
    uiOutput("opt.y") #value comes from Server.R    
    ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations
  mainPanel(
    tabsetPanel(
      tabPanel("Table", tableOutput("view")),
      tabPanel("BasicGraph", plotOutput("plotBasic"))
    )
  )
))

如果你从一个你知道的列表开始,可以使用因子或as.Date函数轻松处理,但是这里我正在接受输入(可以假设格式是mm-yyyy)而且我不知道如何将x变量数据的列设置为变量。这是因为用户可以选择导入数据中的任何列作为X列。

enter image description here

0 个答案:

没有答案
相关问题