Plot two graphs in same plot in R and MarkDown, I have problem with the past answers

905 Views Asked by At

I would like to plot cc and cc2 in the same plot. In my Markdown sheet

I have tried these solutions. From the answer of other users.

---
title: "MWS"
output: html_document
---

```{r pressure, echo=FALSE}
pp<-seq(from =1 , to =10)
cc<- rep(1,10)
cc2<- cos(0.5*cc) 
#first try
plot(pp,cc)
par(new=TRUE)
plot(pp,cc2)

#second try

plot(pp,cc,type="l")
lines(pp,cc2)
plot(pressure)
```

In the first case, my error is "calling par(new=TRUE) with no plot"

In the second case my error is "Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet"

I have also tried the third try

This is the error message with an almost empty MarkDown sheet

On the third try, the error is the same.

1

There are 1 best solutions below

6
Karolis Koncevičius On

In such cases it's best not to use high-level plotting functions, like plot(), at all. Instead, try:

pp  <- seq(from=1, to=10)
cc  <- rep(1, 10)
cc2 <- cos(0.5*cc)

plot.new()
plot.window(xlim=range(pp), ylim=range(cc, cc2))

lines(pp, cc,  col=1)
lines(pp, cc2, col=2)