Plot two graphs in same plot in R

前端 未结 16 844
感情败类
感情败类 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:01

    Idiomatic Matlab plot(x1,y1,x2,y2) can be translated in R with ggplot2 for example in this way:

    x1 <- seq(1,10,.2)
    df1 <- data.frame(x=x1,y=log(x1),type="Log")
    x2 <- seq(1,10)
    df2 <- data.frame(x=x2,y=cumsum(1/x2),type="Harmonic")
    
    df <- rbind(df1,df2)
    
    library(ggplot2)
    ggplot(df)+geom_line(aes(x,y,colour=type))
    

    Inspired by Tingting Zhao's Dual line plots with different range of x-axis Using ggplot2.

    0 讨论(0)
  • 2020-11-22 04:05

    You could use the ggplotly() function from the plotly package to turn any of the gggplot2 examples here into an interactive plot, but I think this sort of plot is better without ggplot2:

    # call Plotly and enter username and key
    library(plotly)
    x  <- seq(-2, 2, 0.05)
    y1 <- pnorm(x)
    y2 <- pnorm(x, 1, 1)
    
    plot_ly(x = x) %>%
      add_lines(y = y1, color = I("red"), name = "Red") %>%
      add_lines(y = y2, color = I("green"), name = "Green")
    

    0 讨论(0)
  • 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:

    0 讨论(0)
  • 2020-11-22 04:08

    When constructing multilayer plots one should consider ggplot package. The idea is to create a graphical object with basic aesthetics and enhance it incrementally.

    ggplot style requires data to be packed in data.frame.

    # Data generation
    x  <- seq(-2, 2, 0.05)
    y1 <- pnorm(x)
    y2 <- pnorm(x,1,1)
    df <- data.frame(x,y1,y2)
    

    Basic solution:

    require(ggplot2)
    
    ggplot(df, aes(x)) +                    # basic graphical object
      geom_line(aes(y=y1), colour="red") +  # first layer
      geom_line(aes(y=y2), colour="green")  # second layer
    

    Here + operator is used to add extra layers to basic object.

    With ggplot you have access to graphical object on every stage of plotting. Say, usual step-by-step setup can look like this:

    g <- ggplot(df, aes(x))
    g <- g + geom_line(aes(y=y1), colour="red")
    g <- g + geom_line(aes(y=y2), colour="green")
    g
    

    g produces the plot, and you can see it at every stage (well, after creation of at least one layer). Further enchantments of the plot are also made with created object. For example, we can add labels for axises:

    g <- g + ylab("Y") + xlab("X")
    g
    

    Final g looks like:

    enter image description here

    UPDATE (2013-11-08):

    As pointed out in comments, ggplot's philosophy suggests using data in long format. You can refer to this answer in order to see the corresponding code.

    0 讨论(0)
  • 2020-11-22 04:10

    Use the matplot function:

    matplot(x, cbind(y1,y2),type="l",col=c("red","green"),lty=c(1,1))
    

    use this if y1 and y2 are evaluated at the same x points. It scales the Y-axis to fit whichever is bigger (y1 or y2), unlike some of the other answers here that will clip y2 if it gets bigger than y1 (ggplot solutions mostly are okay with this).

    Alternatively, and if the two lines don't have the same x-coordinates, set the axis limits on the first plot and add:

    x1  <- seq(-2, 2, 0.05)
    x2  <- seq(-3, 3, 0.05)
    y1 <- pnorm(x1)
    y2 <- pnorm(x2,1,1)
    
    plot(x1,y1,ylim=range(c(y1,y2)),xlim=range(c(x1,x2)), type="l",col="red")
    lines(x2,y2,col="green")
    

    Am astonished this Q is 4 years old and nobody has mentioned matplot or x/ylim...

    0 讨论(0)
  • 2020-11-22 04:10

    If you are using base graphics (i.e. not lattice/ grid graphics), then you can mimic MATLAB's hold on feature by using the points/lines/polygons functions to add additional details to your plots without starting a new plot. In the case of a multiplot layout, you can use par(mfg=...) to pick which plot you add things to.

    0 讨论(0)
提交回复
热议问题