nilsinelabore
nilsinelabore

Reputation: 5095

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?

Upvotes: 2

Views: 425

Answers (2)

Quinten
Quinten

Reputation: 41275

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

Upvotes: 1

nilsinelabore
nilsinelabore

Reputation: 5095

Use add=TRUE as shown below:

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,  add=TRUE)
par(new=TRUE)
curve(dweibull(x, shape=1.5, scale = 1), from=0, to=5, add=TRUE)

enter image description here

Upvotes: 0

Related Questions