Search code examples
rplotcurve

Plot multiple curves in the same plot in R


I want to plot 3 curves in the same plot:

curve(dweibull(x, shape=0.5, scale = 1), from=0, to=5)
par(new=TRUE)
curve(dweibull(x, shape=1, scale = 1), from=0, to=5)
par(new=TRUE)
curve(dweibull(x, shape=1.5, scale = 1), from=0, to=5)

I am aware that using par(new=TRUE) will plot the curves in the same plot but I had three y-axes smeared together, as below: enter image description here

How do I get one clear y-axis?


Solution

  • A ggplot option using geom_function like this:

    library(ggplot2)
    ggplot() + 
      geom_function(fun = function(x) sin(x)) +
      geom_function(fun = function(x) cos(x)) +
      geom_function(fun = function(x) 3*x) +
      labs(x = "x", y = "y") +
      theme_bw()
    

    Created on 2022-08-27 with reprex v2.0.2