Plot two graphs in same plot in R

前端 未结 16 830
感情败类
感情败类 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.

提交回复
热议问题