What always confused me about plotting is the difference between curve and lines. Look how messed up the vertical axis labels are! Since the ranges are different you would need to set ylim=c(lowest point between the two functions, highest point between the two functions), which is less easy than what I'm about to show you-and way less easy if you want to add not just two curves, but many. The output of plot(sin) par(new=T) plot( function(x) x**2 ). I disagree with par(new=TRUE) because that will double-print tick-marks and axis labels. Tl dr: You want to use curve (with add=TRUE) or lines. You can refer to this answer in order to see the corresponding code. For example, we can add labels for axises: g <- g + ylab("Y") + xlab("X")Īs pointed out in comments, ggplot's philosophy suggests using data in long format. Further enchantments of the plot are also made with created object. G produces the plot, and you can see it at every stage (well, after creation of at least one layer). G <- g + geom_line(aes(y=y2), colour="green") Say, usual step-by-step setup can look like this: g <- ggplot(df, aes(x)) With ggplot you have access to graphical object on every stage of plotting. Here + operator is used to add extra layers to basic object. Geom_line(aes(y=y2), colour="green") # second layer Geom_line(aes(y=y1), colour="red") + # first layer
![drawing two curves on one graph r drawing two curves on one graph r](https://i.stack.imgur.com/l6Bg5.png)
![drawing two curves on one graph r drawing two curves on one graph r](https://i.stack.imgur.com/79HBW.jpg)
![drawing two curves on one graph r drawing two curves on one graph r](https://community-cdn.rstudio.com/uploads/default/optimized/2X/d/d243944345839eb4c781be29b73a67344caceb07_2_1035x621.jpeg)
Ggplot(df, aes(x)) + # basic graphical object Ggplot style requires data to be packed in ame. The idea is to create a graphical object with basic aesthetics and enhance it incrementally. When constructing multilayer plots one should consider ggplot package.