【问题标题】:Plotly reactive x axis date format绘制反应x轴日期格式
【发布时间】:2017-07-22 23:41:10
【问题描述】:

我正在与ggplot2plotly 合作。我希望我的 x 轴对正在显示的日期范围做出反应。因此,如果用户正在查看 1990 - 2015 年,我希望 x 轴显示年份。

现在,当用户刷机并调整绘图以显示用户时,比如说 1990 - 1991 年,我希望 x 轴显示月份。

我需要帮助设置这个反应部分。有什么想法吗?

【问题讨论】:

    标签: r ggplot2 shiny plotly axis-labels


    【解决方案1】:

    使用来自ggplotscale_x_date

    library(ggplot2)
    library(plotly)
    library(shiny)
    
    ui <- fluidPage(
      dateRangeInput("SDates", label = ("Select range"), 
                     start="2006-01-01",
                     min=min(economics$date),max=max(economics$date), 
                     format="dd.mm.yyyy",
                     end = "2007-01-01"),
      plolytOutput("plot")
    
    )
    
    server <- function(input, output, session) {
    
      df<-reactive({
        economics[economics$date<input$SDates[2]&economics$date>input$SDates[1]]
      })
      output$plot <- renderPlotly({
        brakes<-ifelse(as.numeric(difftime(input$SDates[2],input$SDates[1], units="days"))>365,"1 year","1 month")
        ldates<-ifelse(as.numeric(difftime(input$SDates[2],input$SDates[1], units="days"))>365,"%Y","%m-%y")
    
        p<-ggplot(df(), aes(x=date, y=unemploy))+geom_line()+
          scale_x_date(date_breaks = brakes, labels = date_format(ldates))
        ggplotly(p)
      })
    
    
    
    
    }
    
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2014-09-04
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多