Draw one or more stories in one window

I want to compare two curves, is it possible for R to draw a graph and then draw another graph? as?

thanks.

+4
source share
6 answers

With base R, you can build your own one curve, and then add a second curve with the lines() argument. Here is a quick example:

 x <- 1:10 y <- x^2 y2 <- x^3 plot(x,y, type = "l") lines(x, y2, col = "red") 

Alternatively, if you want to use ggplot2, here are two methods: one graph displays different colors in the same area, and the other generates separate graphs for each variable. The trick here is to first β€œmelt” the data in a long format.

 library(ggplot2) df <- data.frame(x, y, y2) df.m <- melt(df, id.var = "x") qplot(x, value, data = df.m, colour = variable, geom = "line") qplot(x, value, data = df.m, geom = "line")+ facet_wrap(~ variable) 
+5
source

Using the grill package :

 require(lattice) x <- seq(-3,3,length.out=101) xyplot(dnorm(x) + sin(x) + cos(x) ~ x, type = "l") 

Lattice curve plot

+4
source

There were already some solutions for you. If you stay with the base package, you should familiarize yourself with the functions plot(), lines(), abline(), points(), polygon(), segments(), rect(), box(), arrows(), ... Take a look at their help files.

You should see the graph from the base package in the form of a panel with the coordinates that you gave it. On this panel, you can draw a whole set of objects with the above functions. They allow you to plot as you want. You must remember that if you are not playing with parameters such as that shown by Dr. G, every call to plot () gives you a new panel. Also keep in mind that things can be plotted over other things, so think about the order you use to create the plots.

See for example:

 set.seed(100) x <- 1:10 y <- x^2 y2 <- x^3 yse <- abs(runif(10,2,4)) plot(x,y, type = "n") # type="n" only plots the pane, no curves or points. # plots the area between both curves polygon(c(x,sort(x,decreasing=T)),c(y,sort(y2,decreasing=T)),col="grey") # plot both curves lines(x,y,col="purple") lines(x, y2, col = "red") # add the points to the first curve points(x, y, col = "black") # adds some lines indicating the standard error segments(x,y,x,y+yse,col="blue") # adds some flags indicating the standard error arrows(x,y,x,y-yse,angle=90,length=0.1,col="darkgreen") 

This gives you:

alt text

+2
source

Look at the steam.

 > ?par > plot(rnorm(100)) > par(new=T) > plot(rnorm(100), col="red") 
+1
source

ggplot2 is a great package for this kind of thing:

 install.packages('ggplot2') require(ggplot2) x <- 1:10 y1 <- x^2 y2 <- x^3 df <- data.frame(x = x, curve1 = y1, curve2 = y2) df.m <- melt(df, id.vars = 'x', variable_name = 'curve' ) # now df.m is a data frame with columns 'x', 'curve', 'value' ggplot(df.m, aes(x,value)) + geom_line(aes(colour = curve)) + geom_point(aes(shape=curve)) 

You get a plot colored by a curve, and with different piont marks for each curve and a nice legend, painlessly without extra work:

alt text

+1
source

Draw multiple curves at once using the matplot function. Help (matplot) more.

0
source

Source: https://habr.com/ru/post/1332150/


All Articles