# ggplot2 other - Plot two graphs in same plot in R

## data from (13)

You can also create your plot using ggvis:

```
library(ggvis)
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
df <- data.frame(x, y1, y2)
df %>%
ggvis(~x, ~y1, stroke := 'red') %>%
layer_paths() %>%
layer_paths(data = df, x = ~x, y = ~y2, stroke := 'blue')
```

This will create the following plot:

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")
plot(x, y2, type = "l", col = "green")
```

But when I do it like this, they are not plotted in the same plot together.

In Matlab one can do `hold on`

, but does anyone know how to do this in R?

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`

...

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.

As described by @redmode, you may plot the two lines in the same graphical device using `ggplot`

. However, the data in that answer was in a 'wide' format, whereas in `ggplot`

it is generally most convenient to keep the data in a data frame in a 'long' format. Then, by using different 'grouping variables' in the `aes`

thetics arguments, properties of the line, such as linetype or colour, will vary according to the grouping variable, and corresponding legends will appear. In this case we can use the `colour`

`aes`

sthetics, which matches colour of the lines to different levels of a variable in the data set (here: y1 vs y2). But first we need to melt the data from wide to long format, using the function 'melt' from `reshape2`

package.

```
library(ggplot2)
library(reshape2)
# original data in a 'wide' format
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
df <- data.frame(x, y1, y2)
# melt the data to a long format
df2 <- melt(data = df, id.vars = "x")
# plot, using the aesthetics argument 'colour'
ggplot(data = df2, aes(x = x, y = value, colour = variable)) + geom_line()
```

`lines()`

or `points()`

will add to the existing graph, but will not create a new window. So you'd need to do

```
plot(x,y1,type="l",col="red")
lines(x,y2,col="green")
```

if you want to split the screen, you can do it like this:

(for example for 2 plots next together)

```
par(mfrow=c(1,2))
plot(x)
plot(y)
```

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.}

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:

**UPDATE (2013-11-08):**

As pointed out in comments, `ggplot`

's philosophy suggests using data in long format.
You can refer to this answer https://.com/a/19039094/1796914 in order to see corresponding code.

we can also use lattice library

```
library(lattice)
x <- seq(-2,2,0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
xyplot(y1 + y2 ~ x, ylab = "y1 and y2", type = "l", auto.key = list(points = FALSE,lines = TRUE))
```

For specific colors

```
xyplot(y1 + y2 ~ x,ylab = "y1 and y2", type = "l", auto.key = list(points = F,lines = T), par.settings = list(superpose.line = list(col = c("red","green"))))
```

You can also use `par`

and plot on the same graph but different axis. Something as follows:

```
plot( x, y1, type="l", col="red" )
par(new=TRUE)
plot( x, y2, type="l", col="green" )
```

If you read in detail about `par`

in `R`

, you will be able to generate really interesting graphs. Another book to look at is Paul Murrel's R Graphics.

You can use points for the overplot, that is.

```
plot(x1, y1,col='red')
points(x2,y2,col='blue')
```

Rather than keeping the values to be plotted in an array, store them in a matrix. By default the entire matrix will be treated as one data set. However if you add the same number of modifiers to the plot, e.g. the col(), as you have rows in the matrix, R will figure out that each row should be treated independently. For example:

```
x = matrix( c(21,50,80,41), nrow=2 )
y = matrix( c(1,2,1,2), nrow=2 )
plot(x, y, col("red","blue")
```

This should work unless your data sets are of differing sizes.

Here's an attempt at the fonts, based on links from the xkcd forums and the `extrafont`

package:

As noted above there is a forum discussion about fonts on the xkcd site: I grabbed the first one I could find, there may be other (better?) options (@jebyrnes posts another source for possible fonts in comments above -- the TTF file is here; someone reported a 404 error for that source, you might alternatively try here or here, substituting those URLs appropriately for `xkcdFontURL`

below; you may have to work a bit harder to retrieve the Github-posted links)

```
xkcdFontURL <- "http://simonsoftware.se/other/xkcd.ttf"
download.file(xkcdFontURL,dest="xkcd.ttf",mode="wb")
```

(This is for quickie, one-off use: for regular use you should put it in some standard system font directory.)

```
library(extrafont)
```

The most useful information about fonts was on the extrafont github site -- this is taken from there

```
font_import(".") ## because we downloaded to working directory
loadfonts()
```

Example taken more or less verbatim from the github site:

```
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme(text=element_text(size=16, family="xkcd"))
ggsave("xkcd_ggplot.pdf", plot=p, width=4, height=4)
## needed for Windows:
## Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.05/bin/gswin32c.exe")
embed_fonts("xkcd_ggplot.pdf")
```