## How to overlay 3 functions on one plot using R

I have three functions and a plot code:

f1 <- function(c){0.187*c-0.000236*c^2+0.194*10-0.00330*100-0.000406*10} f2 <- function(c){0.187*c-0.000236*c^2+0.194*16.53-0.00330*(16.53^2)-0.000406*16.53} f3 <- function(c){0.187*c-0.000236*c^2+0.194*20-0.00330*400-0.000406*20}

I wish to plot all three of these on the same graph. I currently have:

png("figure.png") plot(f1(1:1000), type="l", xlab="x", ylab="y", main="the plot :)") plot(f2(1:1000), type="l", xlab="x", ylab="y", add = T) dev.off()

So far this produces just *f1* on a plot as opposed to *f1* and *f2*. I believe I am taking the wrong approach because I am producing **another plot** and trying to add it to a pre-existing plot. I am unsure whether to use geom_line or something similar and just overlay it.

Is there a straight forward way to plot multiple functions and overlay them in the same plot?

`geom_line`

is for `ggplot2`

, which is an entirely different plotting system.

If you start with `plot()`

, you can use `lines()`

to draw lines on your current plot. Your lines are pretty close together, so it doesn't matter much here, but with base `plot`

you usually want to calculate the maximum range in advance so your can set your plot window up right from the start:

x = 1:1000 y1 = f1(x) y2 = f2(x) y3 = f3(x) y_range = range(c(y1, y2, y3)) plot(x, y1, ylim = y_range, type="l", xlab="x", ylab="y", main="the plot :)", col = "red") lines(x, y2, col = "blue") lines(x, y3, col = "chartreuse")

`ggplot2`

is made to work with data in data frames - particularly long-format data frames. Here's how we might approach the problem with `ggplot`

. (Note that, unlike above, `ggplot`

calculates the plot limits and gives a nice legend automatically.)

library(ggplot2) dd = data.frame(x, y1, y2, y3) d_long = reshape2::melt(data = dd, id.vars = "x", variable.name = "fun", value.name = "y") ggplot(d_long, aes(x = x, y = y, color = fun)) + geom_line()

**R tutorials, multiple curves, multiple plots, same plot R,** The lines() function creates curves by joining a sequence of given points with line segments. In the R script shown below, we plot the curves of three data sets (x,y1) Multiple curves on the same plot. To plot more than one curve on a single plot in R, we proceed as follows. Create the first plot using the plot () function. For the subsequent plots, do not use the plot () function, which will overwrite the existing plot. Instead, each one of the subsequent curves are plotted using points () and lines () functions, whose calls are similar to the plot () .

OR sticking with base R plotting like your code, you can just add the extra functions using `lines`

plot(f1(1:1000), type="l", xlab="x", ylab="y", main="the plot :)") lines(1:1000, f2(1:1000)) lines(1:1000, f3(1:1000))

**Combining Plots,** R makes it easy to combine multiple plots into one overall graph, using either the par( ) or layout( ) function. With the par( ) function, you can include the option mfrow=c(nrows, ncols) to create a 3 figures arranged in 3 rows and 1 column R makes it easy to combine multiple plots into one overall graph, using either the par () or layout () function. With the par () function, you can include the option mfrow=c (nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. mfcol=c (nrows, ncols) fills in the matrix by columns.

If you want two plots one right next to the other, you have to set the parameter of your palette. Use par(mfrow=c(1,2)) after the png() command.

png("figure.png") par(mfrow=c(1,2)) plot(f1(1:1000), type="l", xlab="x", ylab="y", main="the plot :)") plot(f2(1:1000), type="l", xlab="x", ylab="y", add = T) dev.off()

**R plot() Function (Add Titles, Labels, Change Colors and Overlaying ,** In this article, you'll learn to use plot() function in R which is used to make For example, the command plot(c(1,2),c(3,5)) would plot the points (1,3) and (2,5) . Create a new Raster* object, based on two or more Raster* objects. (You can also use a single object, but perhaps &version=3.1-5" data-mini-rdoc="raster::calc">calc</a></code> is what you are looking for in that case).</p> <p>You should supply a function <code>fun</code> to set the way that the RasterLayers are combined.

For functions you can also use `curve`

:

f1 <- function(c){0.187*c-0.000236*c^2+0.194*10-0.00330*100-0.000406*10} f2 <- function(c){0.187*c-0.000236*c^2+0.194*16.53-0.00330*(16.53^2)-0.000406*16.53} f3 <- function(c){0.187*c-0.000236*c^2+0.194*20-0.00330*400-0.000406*20} c0 <- 1 c <- 1000 curve(f1, c0, c, main = 'the plot :)', xlab = 'x', ylab = 'y') curve(f2, c0, c, add = T) curve(f3, c0, c, add = T)

As @Gregor noted, `geom_line()`

is a `ggplot()`

call. To go all into the `tidyverse`

, you can do:

#or with ggplot / geom_line library(tidyverse) map_df(list(f1 =f1,f2 = f2,f3 = f3), exec, 1:1000)%>% mutate(x = 1:1000)%>% gather(key = fx,value = value, -x)%>% ggplot(aes(x = x, y = value, col = fx)) + geom_line()

Finally, you may be interested in `facet_grid`

as well:

map_df(list(f1 =f1,f2 = f2,f3 = f3), exec, 1:1000)%>% mutate(x = 1:1000)%>% gather(key = fx,value = value, -x)%>% ggplot(aes(x = x, y = value)) + geom_line() + facet_grid(rows = vars(fx))

**Draw 2 Graphs in Same Plot (R Example),** Draw Multiple Graphs & Lines in Same Plot in R (Example). This article shows how to draw Step 3: Draw Overlaying Line to Plot. We can also mix our original Overlaying Plots Using legend() function. Calling plot() multiple times will have the effect of plotting the current graph on the same window replacing the previous one. However, sometimes we wish to overlay the plots in order to compare the results. This is made possible with the functions lines() and points() to add lines and points

**Combine Multiple GGPlots in One Graph - Articles,** R function: ggparagraph() [in ggpubr]. We finish by arranging/combining the three plots using the function ggarrange() [in ggpubr] # Density plot of 1 If you'd like to overlay the ROC curves over each other, you can use the roc function from the pROC R package to get the sensitivity and specificity values and plot them out manually, #outcome var y = c (rep (0,50), rep (1, 50))

**ggplot2,** You need to install the R package ggpubr (version >= 0.1.3), to easily create Here, we'll use ggplot2-based plotting functions available in ggpubr. You can use Another example, overlaying the France map and a ggplot2: Overlay the graphics by using Show: Display the full plot range using PlotRange -> All : Graphics can also be placed together in a row, grid, or column if you do not want to overlay them.

**The curve Function,** The curve function takes, as its first argument, an R expression. previous plot. For example, if we wanted to overlay the function y=x^2 on top of y=x we could type: We could plot some data and then use curve to draw a y=x line on top of it: set.seed(1) x <- rnorm(100) y <- x + rnorm(100) plot(y ~ x) curve((x), add = TRUE). Then call a plotting function to plot into the axes. For example, create two plots in a 2-by-1 layout. Add a title to each plot. Note: This code uses the tiledlayout function, which is available starting in R2019b. If you are using an earlier release, use the subplot function instead.