以x轴为日期的闪亮ggplot绘图笔刷限制 [英] Shiny ggplot plot brush limits with x axis as date

查看:54
本文介绍了以x轴为日期的闪亮ggplot绘图笔刷限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制rstudio页面上提供的示例.

I am trying to replicate this example provided on the rstudio page.

画廊/plot-interaction-zoom

问题是我需要为x轴使用日期( as.Date%d/%m/%Y ),并且在绘图上放大时会出现此错误

The problem is that I need to use date (as.Date %d/%m/%Y) for the x axis and When zooming on the plot I get this error

无效的输入:date_trans仅适用于Date类的对象

Invalid input: date_trans works with objects of class Date only

library(ggplot2)
library(scales)
library (grid)
library (DT)
ui <- fluidPage(
fluidRow(
column(width = 12, class = "well", h4("Left plot controls right plot"),
fluidRow(column(width = 6, plotOutput("plot1", height = 300,
brush = brushOpts(
id = "plot2_brush",
esetOnNew = TRUE)))
,
column(width = 6,plotOutput("plot2", height = 300,
click = "plot_click",
dblclick = dblclickOpts(
id = "plot_dblclick")))
,
fluidRow(column(width = 12, dataTableOutput("selected_rows")))
))))

Date <- c("01/01/2014","01/01/2014","01/01/2014","01/01/2014")
Sevdow <- c(10,30,10,50)
articles<-as.data.frame(cbind(dates, Sevdow))
articles$Sevdow<-as.numeric(tt$Sevdow)
articles$Date<-as.Date(tt$dates, format="%d/%m/%Y")

server <- function(input, output) {
ranges2 <- reactiveValues(x = NULL, y = NULL
output$plot1 <- renderPlot({
ggplot(articles, aes(Date, Sevdow)) +
geom_point()
})

output$plot2 <- renderPlot({
ggplot(articles, aes(Date, Sevdow)) +
geom_point() +
coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
})

observe({
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)

} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})

output$selected_rows <- renderDataTable({
datatable(
nearPoints(articles, input$plot_click, threshold = 10, maxpoints = 1000,
addDist = FALSE, allRows = FALSE))
})
}

shinyApp(ui, server)

推荐答案

您好,您需要将 range2 $ x 转换为Date:

Hello you need to convert range2$x to Date :

output$plot2 <- renderPlot({
  if (!is.null(ranges2$x)) {
    ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
  }
  ggplot(articles, aes(Date, Sevdow)) +
    geom_point() +
    coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
})

对于 nearPoints ,您需要将 articles $ Date 转换为数字:

And for nearPoints you need to convert articles$Date to numeric :

output$selected_rows <- renderDataTable({
  articles$Date <- as.numeric(articles$Date)
  nearPoints(articles, coordinfo = input$plot_click, xvar = "Date", yvar = "Sevdow", 
             threshold = 10, maxpoints = 1000,
             addDist = FALSE, allRows = FALSE)
})

这篇关于以x轴为日期的闪亮ggplot绘图笔刷限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆