Ameya
Ameya

Reputation: 1730

R - Force plotly to plot missing values

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).

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))

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

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'))

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!

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.

Upvotes: 4

Views: 5723

Answers (1)

Florian
Florian

Reputation: 25405

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))))

enter image description here

Hope this helps!

Upvotes: 6

Related Questions