Plot two graphs in same plot in R

前端 未结 16 840
感情败类
感情败类 2020-11-22 03:37

I would like to plot y1 and y2 in the same plot.

x  <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
plot(x, y1, type = \"l\", col = \"red\")         


        
16条回答
  •  心在旅途
    2020-11-22 04:05

    Using plotly (adding solution from plotly with primary and secondary y axis- It seems to be missing):

    library(plotly)     
    x  <- seq(-2, 2, 0.05)
    y1 <- pnorm(x)
    y2 <- pnorm(x, 1, 1)
    
    df=cbind.data.frame(x,y1,y2)
    
      plot_ly(df) %>%
        add_trace(x=~x,y=~y1,name = 'Line 1',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE) %>%
        add_trace(x=~x,y=~y2,name = 'Line 2',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE,yaxis = "y2") %>%
        layout(title = 'Title',
           xaxis = list(title = "X-axis title"),
           yaxis2 = list(side = 'right', overlaying = "y", title = 'secondary y axis', showgrid = FALSE, zeroline = FALSE))
    

    Screenshot from working demo:

提交回复
热议问题