如何避免在R中以绘图方式绘制丢失的日期 [英] How to avoid plotting missing dates in R plotly

查看:57
本文介绍了如何避免在R中以绘图方式绘制丢失的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制一个面积图,其中包含财务数据,而我正在使用plotly进行绘制. 问题是,plotly具有检测时间序列格式的功能,并且只需简单地延伸一条线性线直到下一个可用的数据输入,就可以绘制甚至丢失的日期. 是否可以禁用此功能并仅绘制可用数据的时间序列?

I need to plot a area chart, it contains financial data and I am using plotly to do so. The problem is, plotly has a feature of detecting the time series format and plots even the missing dates by simply extending a linear line until the next available data input. Is it possible to disable this feature and plot only the time series where data is available?

library(plotly)

    Datetime <- c(
      "2016-01-05 00:00:00",
      "2016-01-06 00:00:00",
      "2016-01-07 00:00:00",
      "2016-01-08 00:00:00",
      "2016-01-11 00:00:00",
      "2016-01-12 00:00:00",
      "2016-01-13 00:00:00",
      "2016-01-14 00:00:00",
      "2016-01-15 00:00:00",
      "2016-01-18 00:00:00",
      "2016-01-19 00:00:00",
      "2016-01-20 00:00:00",
      "2016-01-21 00:00:00",
      "2016-01-22 00:00:00",
      "2016-01-25 00:00:00",
      "2016-01-26 00:00:00",
      "2016-01-27 00:00:00",
      "2016-01-28 00:00:00",
      "2016-01-29 00:00:00",
      "2016-02-01 00:00:00")
plotdata <- c(93763,110023,134873,138780,117038,117890,120025,140715,48567,87592,
              115852,145189,162258,121456,93643,128475,119310,105771,134946,90386)

volume_data <- data.frame(Datetime, plotdata)
plot_ly(volume_data, x = Datetime, y = plotdata, type = "bar")

这是基本的示例数据,如果执行此操作,则会注意到图中有空白. 尽管在执行过程中,我使用的是面积图,但我还是用条形图展示了一个示例数据,以便更容易注意到空白. 我了解可以将x轴数据识别为时间序列,并自动完成丢失的数据.是否可以禁用此功能并仅绘制可用数据的日期和时间?

This is a basic sample data, If you execute this, you will notice that there are blank spaces in the graph. Though in my execution, I am using area chart, I have presented a sample data with bar chart so that the blank spaces are easier noticed. I understand that plotly recognises the x axis data as timeseries and auto completes the missing data. Is it possible to disable this and plot only the date and time where the data is available?

推荐答案

这里是可以为您提供所需内容的替代方法.

Here is an alternative that will give you what you want.

p <- plot_ly(
  x = c("Jan 5", "March 5", "April 5"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar")
p %>% layout(xaxis = list(title="Date"), yaxis = list(title="Volume Data"))

在绘制之前,您只需要转换Datetime列(例如1月5日,3月5日...格式)

You just need to convert your Datetime column (like Jan 5, March 5...format) prior to plotting

这篇关于如何避免在R中以绘图方式绘制丢失的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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