0

I have two files that I want to plot in the graph. Below are the files:

File 1:

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

File 2:

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

PROBLEM:

1 The first file has irregular intervals, and the second File has regular intervals of 100. I have multiple files similar to File 1 (also with irregular intervals). How do I plot these 2 files in 1 figure? I want to specify the interval when plotting to 100.

I tried plotting this in R using the plot() option and par(new=T). The result si as follow:

Sample output

The y-axis appears to plot on top of each other.

[2] I want to plot the y-axis as inverted with 1000 at the lowest level.

[3] Is there a way to do this in ggplot?

Any suggestions on how to do this in R?

I'll appreciate any help.

zx8754
  • 42,109
  • 10
  • 93
  • 154
Lyndz
  • 773
  • 1
  • 10
  • 25
  • 3
    Too many questions for one post, but here is the start: define your xlim and ylims based on both datasets, then use `plot(data1... xlim, ylim)` for data1, then use `points(data2...)` for data2. – zx8754 Feb 01 '19 at 08:21
  • thanks!!!this works..Is there a way to do this using ggplot?I updated my question – Lyndz Feb 01 '19 at 08:27
  • 1
    Yes it can be done with ggplot: merge/rbind both datasets, then plot. – zx8754 Feb 01 '19 at 08:31

1 Answers1

1

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

Saurabh Chauhan
  • 2,833
  • 1
  • 13
  • 37