Ay_M
Ay_M

Reputation: 203

Multiple Plots in R

I want to plot 2 graphs in 1 frame. Basically I want to compare the results.

Anyways, the code I tried is:

    plot(male,pch=16,col="red")
    lines(male,pch=16,col="red")
    par(new=TRUE)
    plot(female,pch=16,col="green")
    lines(female,pch=16,col="green")

When I run it, I DO get 2 plots in a frame BUT it changes my y-axis. Added my plot below. Anyways, y-axis values are -4,-4,-3,-3,... It's like both of the plots display their own axis. enter image description here

Please help.

Thanks

Upvotes: 0

Views: 785

Answers (2)

Thilo
Thilo

Reputation: 9167

For this kind of plot I usually like the plotting with ggplot2 much better. The main reason: It generalizes nicely to more than two lines without a lot of code.

The drawback for your sample data is that it is not available as a data.frame, which is required for ggplot2. Furthermore, in every case you need a x-variable to plot against. Thus, first let us create a data.frame out of your data.

dat <- data.frame(index=rep(1:10, 2), vals=c(male, female), group=rep(c('male', 'female'), each=10))

Which leaves us with

> dat
   index          vals  group
1      1 -0.4334269341   male
2      2  0.8829902521   male
3      3 -0.6052638138   male
4      4  0.2270191965   male
5      5  3.5123679143   male
6      6  0.0615821014   male
7      7  3.6280155376   male
8      8  2.3508890457   male
9      9  2.9824432680   male
10    10  1.1938052833   male
11     1  1.3151289227 female
12     2  1.9956491556 female
13     3  0.8229389822 female
14     4  1.2062726250 female
15     5  0.6633392820 female
16     6  1.1331669670 female
17     7 -0.9002109636 female
18     8  3.2137052284 female
19     9  0.3113656610 female
20    10  1.4664434215 female

Note that my command assumes you have 10 data values each. That command would have to be adjusted according to your actual data.

Now we may use the mighty power of ggplot2:

library(ggplot2)
ggplot(dat, aes(x=index, y=vals, color=group)) + geom_point() + geom_line()

The call above has three elements: ggplot initializes the plot, tells R to use dat as datasource and defines the plot aesthetics, or better: Which aesthetic properties of the plot (such as color, position, size, etc.) are influenced by your data. We use the x and y-values as expected and furthermore set the color aesthetic to the grouping variable - that makes ggplot automatically plot two groups with different colors. Finally, we add two geometries, that pretty much do what is written above: Draw lines and draw points.

The result:

Plot of your data using ggplot

If you have your data saved in the standard way in R (in a data.frame), you end with one line of code. And if after some thousands years of evolution you want to add another gender, it is still one line of code.

Upvotes: 1

Victor K.
Victor K.

Reputation: 4094

You don't need the second plot. Just use

> plot(male,pch=16,col="red")
> lines(male, pch=16, col = "red")
> lines(female, pch=16, col = "green")
> points(female, pch=16, col = "green")

Note: that will set the frame boundaries based on the first data set, so some data from the second plot could be outside the boundaries of the plot. You can fix it by e.g. setting the limits of the first plot yourself.

Upvotes: 4

Related Questions