Search code examples
rtime-seriesplotlycontour

stacked plots in R plot_ly with shared x axis that doesn't match up


I am trying to make stacked plots that look like this but with a shared x axis that lines up: stacked charts of scatterplot on top and contour plot below that share an x date axis

When I try my code the axes don't seem to line up even though the date columns are the same class ("Date") and extents. This code:

 p3soiH <- structure(list(SOI_d2H_P3_60 = c(0.270750353, 0.230147073, 0.181060466,
0.14969623, 0.217420949, 0.27484718, 0.245556866, 0.253293044,
0.297027026, 0.230037639), SOI_d2H_P3_30 = c(0.317495676, 0.33832448,
0.321248762, 0.322584764, 0.309468651, 0.378476382, 0.374018999,
0.385795817, 0.401733649, 0.327856546), SOI_d2H_P3_10 = c(0.47774046,
0.505443033, 0.477832219, 0.515614846, 0.513315096, 0.542324327,
0.568186111, 0.590708983, 0.622258143, 0.577638524), SOI_d2H_P3_05 = c(1.065008756,
1.029251905, 1.079236783, 0.972995461, 1.054741593, 1.020166422,
1.110624711, 1.164420391, 1.138018061, 1.139206468), date = structure(c(19193,
19194, 19195, 19196, 19197, 19198, 19199, 19200, 19201, 19202
), class = "Date")), row.names = 35:44, class = "data.frame")


dly.met.fp <- structure(list( dly_rain_mm = c(1.524, 0, 0, 1.27, 3.048, 4.064, 0, 7.62,
1.27, 10.922), day_asdate = structure(c(19193,
19194, 19195, 19196, 19197, 19198, 19199, 19200, 19201, 19202
), class = "Date")), row.names = 35:44, class ="data.frame")

#make scatter plot of date and hourly rainfall
rain.plot <- plot_ly(x = dly.met.fp$day_asdate,y = dly.met.fp$dly_rain_mm , type = "scatter", mode = "markers") 

print(rain.plot)

# make contour plor of SOI
cont.plot <- plot_ly(x = p3soiH$date, y =c(5,10,30,60) ,
                     z = as.matrix(p3soiH[,4:1]), type = "contour") 
print(cont.plot)

# stack plots and share x axis (dates)
fig <- subplot( rain.plot, cont.plot,nrows = 2, shareX = TRUE)

# print combined figure
print(fig)

And I end up with this where the contour plot and scatter plot axes don't match: enter image description here

I was able to achieve it using synthetic data from the volcano dataset, but not sure how the dates are different. I also tried using as.numeric(date) for each "date" x-axis and it gave the same result. Perhaps R plotly uses dates differently internally for contour and scatter plots?


Solution

  • The reason for your error is that z is only 4 columns, so you only get 4 days.

    Here, I just repeat the z and it goes up to the 29th.

    plot_ly(data = p3soiH, 
                         x = ~date, y = c(5,10,30,60),
                         z = as.matrix(rep(p3soiH[,4:1]), 2),
                         type = "contour") 
    

    You would need to adjust your z column to your actual data to ensure you are getting the alignment on # of days.

    You can show this by doing just your snippet:

    # Only 4 days!
    plot_ly(data = p3soiH, 
                         x = ~date, y = c(5,10,30,60),
                         z = as.matrix(p3soiH[,4:1]),
                         type = "contour")
    

    This solves the visual of your problem, but you would need to sort out what z means for your context and how to visualize it appropriately. You could for example transpose your matrix to be 4 rows and 10 columns But I wouldn't know if that is meaningful for your data.

    # transpose to 4 rows, 10 columns; 1 column per date. 
    plot_ly(data = p3soiH, 
                         x = ~date, y = c(5,10,30,60),
                         z = t(as.matrix(p3soiH[,4:1])),
                         type = "contour")