Shiny / R:从字符日期格式化图表日期

时间:2021-04-09 14:29:14

I have spent hours on this and I am a new user. I've been to many websites and tried different things. I can not seem to get the graph to show dates in a YYYY-MM format on the x axis. The date starts out being character, which seems to work for the date range inputs. But the graph date is either showing as a long number or as Mar-01, Apr-01, etc. I'd rather see 2017-Feb, 2017-Mar, 2017-Apr, 2017-May. IF they would not sort appropriately like this I would need 2017-02, 2017-03, etc. (or even 201702 - just some year then month format). My app has several dynamic UI, which are working. What I'm showing is a simple version but I left the date range pick in just in case that is relevant.

我花了好几个小时,我是一个新用户。我去过很多网站,尝试过不同的东西。我似乎无法让图表在x轴上以YYYY-MM格式显示日期。日期开始是字符,似乎适用于日期范围输入。但图表日期要么显示为长数字,要么显示为Mar-01,Apr-01等。我宁愿看2017年2月,2017年3月,2017年4月,2017年5月。如果他们不适合这样排序,我需要2017-03-03等等(甚至201702年 - 只需一年的月份格式)。我的应用程序有几个动态UI,它们正在运行。我所展示的是一个简单的版本,但我保留了日期范围选择,以防万一。

   library(shiny)
library(tidyverse)
library(DT)
library(lubridate)

data_source_end = c("2/28/2017","3/31/2017", "4/30/2017", "5/31/2017")

mean_score = c(1.5, 2.5 , 3.5, -.5)

df <- data.frame(data_source_end, mean_score)
df$data_source_char <- as.character(df$data_source_end)
df$data_source_mdy <- as.Date(mdy(df$data_source_char),"%Y-%m-%d")

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      dateRangeInput("dates","DateRange",
                               start = min(df$data_source_char),
                               end = max(df$data_source_char))
      ),

    mainPanel( 
      plotOutput("plot2")
  )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$DateRange <- renderText({
    #i have  validate messages here 
  })

  fildata <- reactive({
    fildata  <-  df
    filtered2 <- fildata %>%
      select(data_source_mdy, mean_score ) %>%
      dplyr::arrange(data_source_mdy)
    return(filtered2)
  })
  output$plot2 <- renderPlot({
    x <-fildata()$data_source_mdy
    y <-  fildata()$mean_score 
    plot(x, y, xlim=c(input$dates[1],input$dates[2] )
    )
  })

}


# Run the application 
shinyApp(ui = ui, server = server)

1 个解决方案

#1


0  

You could use axis.Date(side = 1, at = x, format = "%Y-%m") to get the date format where format parameter %Y-%m specifies YYYY-MM format.

您可以使用axis.Date(side = 1,at = x,format =“%Y-%m”)来获取格式参数%Y-%m指定YYYY-MM格式的日期格式。

Your output$plot2 would look something like this:

你的输出$ plot2看起来像这样:

output$plot2 <- renderPlot({
      x <-fildata()$data_source_mdy
      y <-  fildata()$mean_score 
      plot(x, y, xlim=c(input$dates[1],input$dates[2]), xaxt = "n")
      axis.Date(side = 1, at = x, format = "%Y-%m")
    }) 

#1


0  

You could use axis.Date(side = 1, at = x, format = "%Y-%m") to get the date format where format parameter %Y-%m specifies YYYY-MM format.

您可以使用axis.Date(side = 1,at = x,format =“%Y-%m”)来获取格式参数%Y-%m指定YYYY-MM格式的日期格式。

Your output$plot2 would look something like this:

你的输出$ plot2看起来像这样:

output$plot2 <- renderPlot({
      x <-fildata()$data_source_mdy
      y <-  fildata()$mean_score 
      plot(x, y, xlim=c(input$dates[1],input$dates[2]), xaxt = "n")
      axis.Date(side = 1, at = x, format = "%Y-%m")
    })