Tuesday 24 September 2019

ggplot2 - Plotting two files with different xintervals in the same graph in R

Solution using plotly:


Your input data:


df=data.frame(structure(c(1000, 935, 925, 903, 868, 850, 805, 797, 759, 738,
734, 700, 683, 666, 567, 562, 500, 487, 461, 416, 400, 393, 372,
364, 357, 338, 333, 329, 315, 300, 283, 280, 263, 250, 231, 200,
189, 176, 150, 141, 119, 104, 103, 101, 100, NA, 21.4, 21.2,
20.4, 19.1, 18.4, 17, 17.8, 17.4, 14.7, 14.2, 13.2, 12, 10.8,
-0.7, -1, -4.9, -5.8, -7.7, -12.5, -14.3, -15.1, -17.5, -18.5,
-19.9, -22.3, -23.1, -23.7, -26, -28.5, -31.5, -32.1, -35.9,
-38.9, -43.5, -51.9, -55.1, -59.1, -67.1, -70, -78.1, -84.5,
-83.3, -80.9, -79.1), .Dim = c(45L, 2L)))
df1= data.frame(structure(c(1000, 950, 900, 850, 800, 750, 700, 650, 600, 550,
500, 450, 400, 350, 300, 250, 200, 150, 100, NA, 25.4, 27.2,
20.5, 18.1, 17.4, 16, 14.2, 13.2, 12, 10.8, -0.7, -1, -4.9, -17.5,
-84.5, -83.3, -80.9, -79.1), .Dim = c(19L, 2L)))

Code:


plot_ly(df) %>%
add_trace(x=df$X1,y=df$X2,name = 'Line 1',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE) %>%
add_trace(x=df1$X1,y=df1$X2,name = 'Line 2',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE,yaxis = "y2") %>%
layout(title = 'Title here',
xaxis = list(title = "X-axis title"),
yaxis2 = list(side = 'right', overlaying = "y", title = 'secondary y axis', showgrid = FALSE, zeroline = FALSE))

Code snippet from working demo:
enter image description here

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...