R - 强制绘制缺失值 [英] R - Force plotly to plot missing values

查看:57
本文介绍了R - 强制绘制缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制多个图,所有图都具有相同的 X 轴.

I want to plot multiple plots, all with the same X-axis.

其中一个图在开始时有一些缺失值(根据定义,就像移动平均数据一样).

One of those plots has a few missing values at the start (by definition, like a moving average data would).

为了可比性,我希望所有绘图的 X 轴起点都相同.

For comparability, I'd like the start of the X-axis for all plots to be the same.

这是一个可重现的例子 -

Here's a reproducible example -

library(plotly)
plotdata <- data.frame(Month=as.POSIXct(c(1309435200, 1312113600, 1314792000, 1317380400, 1320058800, 
                                     1322650800, 1325329200, 1328007600, 1330513200, 1333191600, 1335787200, 
                                     1338465600, 1341057600, 1343736000, 1346414400, 1349002800, 1351681200, 
                                     1354273200, 1356951600, 1359630000, 1362049200, 1364727600, 1367323200, 
                                     1370001600, 1372593600, 1375272000, 1377950400, 1380538800, 1383217200, 
                                     1385809200, 1388487600, 1391166000, 1393585200, 1396263600, 1398859200, 
                                     1401537600, 1404129600, 1406808000, 1409486400, 1412074800, 1414753200, 
                                     1417345200, 1420023600, 1422702000, 1425121200, 1427799600, 1430395200, 
                                     1433073600, 1435665600, 1438344000, 1441022400, 1443610800, 1446289200, 
                                     1448881200, 1451559600, 1454238000, 1456743600, 1459422000, 1462017600, 
                                     1464696000, 1467288000, 1469966400, 1472644800, 1475233200, 1477911600, 
                                     1480503600, 1483182000, 1485860400, 1488279600, 1490958000, 1493553600
), origin='1970-01-01'), Value=c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.02, 0.05, 
                                     0.06, 0.04, 0.06, 0.06, 0.03, 0.04, 0.07, 0.03, 0.08, 0.05, 0.08, 
                                     0.09, 0.06, 0.05, 0.04, 0.08, 0.1, 0.07, 0.01, 0.11, 0.07, 0.04, 
                                     0.05, 0.05, 0.03, 0.05, 0.04, 0.02, 0.02, 0.05, 0.1, 0.01, 0.03, 
                                     0.03, 0.05, 0.03, 0.05, 0.05, 0.05, 0.08, 0.08, 0.1, 0.07, 0.07, 
                                     0.06, 0.08, 0.11, 0.06, 0.08, 0.07, 0.08, 0.08, 0.08, 0.03, 0.02, 
                                     0.13, 0.07))

现在,我不能强制包含前 12 个点,因为它们是 NA.

Now, I can't force plotly to include the first 12 points because they are NA.

文档建议使用 tickvals、ticktext.但这似乎不起作用.也不会试图强制 X 轴从 2011-07-01(第一个月)开始.这是我尝试过的 -

The documentation suggests using tickvals, ticktext. But that doesn't seem to work. Nor does trying to force the X-axis to start at 2011-07-01 (the first month). Here's what I have tried -

plot_ly(plotdata, x=~Month, y=~Value, type='scatter', mode='lines') %>% layout(xaxis=list(tickmode='array', ticktext=plotdata$Time, tickvals=plotdata$Time))
plot_ly(plotdata, x=~Month, y=~Value, type='scatter', mode='lines') %>% layout(xaxis=list(tick0='2011-07-01', dtick='M12'))

两个图都生成了从 2012 年 7 月开始的 X 轴,而不是我想要的 2011 年 7 月.

Both graphs produce the X-axis beginning from July 2012 as opposed to July 2011 as I would want.

我应该如何强制执行所需的行为?谢谢!

How should I force the required behaviour from plotly? Thank you!

附言我不想将 Month 列转换为因子/字符,以便我可以在布局中使用日期格式选项.

P.S. I'd like to not convert the Month column into a factor/character so that I can use the date formatting options in the layout.

推荐答案

您可以通过设置范围来实现:

You could do this by setting the range:

plot_ly(plotdata, x=~Month, y=~Value, type='scatter', mode='lines') %>%
  layout(xaxis=list(range = c(min(plotdata$Month),max(plotdata$Month))))

希望这会有所帮助!

这篇关于R - 强制绘制缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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