Plot two graphs in same plot in R

前端 未结 16 841
感情败类
感情败类 2020-11-22 03:37

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


        
相关标签:
16条回答
  • 2020-11-22 03:54

    I think that the answer you are looking for is:

    plot(first thing to plot)
    plot(second thing to plot,add=TRUE)
    
    0 讨论(0)
  • 2020-11-22 03:54

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

    enter image description here

    0 讨论(0)
  • 2020-11-22 03:54

    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.

    0 讨论(0)
  • 2020-11-22 03:57

    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")
    
    0 讨论(0)
  • 2020-11-22 04:00

    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

    sine and parabola

    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.

    id & sine

    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.

    0 讨论(0)
  • 2020-11-22 04:01

    You can use points for the overplot, that is.

    plot(x1, y1,col='red')
    
    points(x2,y2,col='blue')
    
    0 讨论(0)
提交回复
热议问题