ChatGPT解决这个技术问题 Extra ChatGPT

Plot two graphs in same plot in R

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?

Check out ?curve. Use add=TRUE.
See this question for more specific ggplot2 answers.

p
phoxis

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")

Why doesn't it work in the following simple example? > plot(sin) > lines(cos) Error in as.double(y) : cannot coerce type 'builtin' to vector of type 'double'
This is easy to see. With plot(sin), you are passing a function instead of actual data. plot() will detect this and in turn use plot.function() to plot your function (read up on multiple dispatch to learn more about this). However, lines.function() is not defined, so lines() doesn't know what to do with a parameter of class function. lines can only deal with your data and time series objects of class ts.
@Frank Do it like this: plot(sin); curve(cos, add=TRUE).
How to use the same if x is different? Say, I have x1 and y1 for one graph and add another graph of x2 and y2 in the same graph. Both x1 and x2 have same range but different values.
What's the most straightforward way to add a legend to this?
4
4 revs, 4 users 70%

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.


My R gives me an error: Error in par(fig(new = TRUE)) : could not find function "fig"
Does your method preserve the right scale (y axis) for the two plots?
@uvts_cvs Yes, it preserves the original graph in toto.
The problem with this is it will rewrite several plot elements. I would include xlab="", ylab="", ... and a few others in the second plot.
can you please look at my question if you have time? stackoverflow.com/questions/65650991/… thanks
N
NelsonGon

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:

https://i.stack.imgur.com/FsPps.png

UPDATE (2013-11-08):

As pointed out in comments, ggplot's philosophy suggests using data in long format. You can refer to this answer in order to see the corresponding code.


As suggested by Henrik, the data really should be in "long" format, ggplot handles this more naturally than the "wide" format you use.
@Henrik: No, thank you for your answer in the first place. Perhaps the author of this answer can edit it so that it fits well with ggplot's philosophy...
taught me defining x on ggplot(aes()) and then y by itself on geom_*(). Nice!
u
user3749764

I think that the answer you are looking for is:

plot(first thing to plot)
plot(second thing to plot,add=TRUE)

This doesn't seem to work, it gives an "add" is not a graphical parameter warning then just prints the second plot over the first one.
One nice benefit of this is that it seems to keep the axes limits and titles consistent. Some of the previous methods cause R to draw two sets of tick marks on the y axis, unless you go through the trouble of specifying more options. Needless to say, having two sets of tick marks on the axes could be very misleading.
the add parameter works for some plot methods, but not the base/default one in R
I got the same error "add" is not a graphical parameter. My R is R version 3.2.3 (2015-12-10). You could use par(new=TRUE) command between these plots.
S
Spacedman

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


The range() function here is especially useful.
i
isomorphismes

tl;dr: You want to use curve (with add=TRUE) or lines.

I disagree with par(new=TRUE) because that will double-print tick-marks and axis labels. Eg

https://i.stack.imgur.com/xx8e1.png

The output of plot(sin); par(new=T); plot( function(x) x**2 ).

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.

What always confused me about plotting is the difference between curve and lines. (If you can't remember that these are the names of the two important plotting commands, just sing it.)

Here's the big difference between curve and lines.

curve will plot a function, like curve(sin). lines plots points with x and y values, like: lines( x=0:10, y=sin(0:10) ).

And here's a minor difference: curve needs to be called with add=TRUE for what you're trying to do, while lines already assumes you're adding to an existing plot.

https://i.stack.imgur.com/VD8Cu.png

Here's the result of calling plot(0:2); curve(sin).

Behind the scenes, check out methods(plot). And check body( plot.function )[[5]]. When you call plot(sin) R figures out that sin is a function (not y values) and uses the plot.function method, which ends up calling curve. So curve is the tool meant to handle functions.


A
Adarsh Ravi

if you want to split the plot into two columns (2 plots next to each other), you can do it like this:

par(mfrow=c(1,2))

plot(x)

plot(y) 

Reference Link


H
Henrik

As described by @redmode, you may plot the two lines in the same graphical device using ggplot. In that answer the data were in a 'wide' format. However, when using 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 aesthetics 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 aessthetics, 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 e.g. the function 'melt' from reshape2 package. Other methods to reshape the data are described here: Reshaping data.frame from wide to long format.

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()

https://i.stack.imgur.com/zZldE.png


p
phoxis

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.


J
Jason Sturges

You can use points for the overplot, that is.

plot(x1, y1,col='red')

points(x2,y2,col='blue')

A
Alessandro Jacopson

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))

https://i.stack.imgur.com/Bf4Sp.png

Inspired by Tingting Zhao's Dual line plots with different range of x-axis Using ggplot2.


c
cranberry

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.


This gives: Error in if (as.factor) { : argument is not interpretable as logical
C
Carson

You could use the ggplotly() function from the plotly package to turn any of the gggplot2 examples here into an interactive plot, but I think this sort of plot is better without ggplot2:

# call Plotly and enter username and key
library(plotly)
x  <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)

plot_ly(x = x) %>%
  add_lines(y = y1, color = I("red"), name = "Red") %>%
  add_lines(y = y2, color = I("green"), name = "Green")

https://i.stack.imgur.com/0Qjjl.png


plotly looks brilliant; is it free ?
@denis, there is unlimited free public plotting and paid private plotting or on-premise options. See the plans page.
The plotly R package is now 100% free and open source (MIT licensed). You can use it with or without a plotly account.
can you please take a look at my question? stackoverflow.com/questions/65650991/… thanks!
e
epo3

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:

https://i.stack.imgur.com/TWfYW.png


S
Saurabh Chauhan

Using plotly (adding solution from plotly with primary and secondary y axis- It seems to be missing):

library(plotly)     
x  <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)

df=cbind.data.frame(x,y1,y2)

  plot_ly(df) %>%
    add_trace(x=~x,y=~y1,name = 'Line 1',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE) %>%
    add_trace(x=~x,y=~y2,name = 'Line 2',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE,yaxis = "y2") %>%
    layout(title = 'Title',
       xaxis = list(title = "X-axis title"),
       yaxis2 = list(side = 'right', overlaying = "y", title = 'secondary y axis', showgrid = FALSE, zeroline = FALSE))

Screenshot from working demo:

https://i.stack.imgur.com/u82W3.png


I compiled the code and does not work, first marked an error in %>% and I deleted it, then marked an error Error in library(plotly) : there is no package called ‘plotly’ why?
Have you installed the package plotly? You need to install the package using install.packages("plotly") command.
V
Varn K

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"))))

https://i.stack.imgur.com/rEmQF.png