2 plots on same graph

345 views
Skip to first unread message

Ace

unread,
Mar 7, 2012, 9:10:14 PM3/7/12
to ggplot2
Hi, newbie here. I need to know how to merge two plots into the same
graph in ggplot2. They both have the same x and y axis. I found an
example of how to do a side by side one but thats not what I want.

Serdar Akin

unread,
Mar 8, 2012, 7:41:49 AM3/8/12
to Ace, ggplot2
Hi,

You can use "viewport" and adjust the necessary parameters, for instance
vp1 <- viewport(width = unit(140,"mm"), height = unit(140,"mm"), y = 0.48, x = 0.38)
vp2 <- viewport(width = unit(29,"mm"), height = unit(121,"mm"), y = 0.495, x = 0.88)
print(graf1, vp = vp1)
print(graf2, vp = vp2)


-----Ursprungligt meddelande-----
Från: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] För Ace
Skickat: den 8 mars 2012 03:10
Till: ggplot2
Ämne: 2 plots on same graph

Hi, newbie here. I need to know how to merge two plots into the same graph in ggplot2. They both have the same x and y axis. I found an example of how to do a side by side one but thats not what I want.

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Mar 8, 2012, 12:32:30 PM3/8/12
to Ace, ggplot2
Hi:

Assuming you want to put two graphs in the same graphics space without
dividing it into sectors and rendering a separate graph in each, there
are several ways one could go. Much depends on how you've arranged
your data and what type of graphic you're trying to construct, which
you've failed to mention.

It would be easier to help if you provided a reproducible example; see
http://gist.github.com/270442 for instructions on how to produce one.
(Hint: it requires code that you've tried and a small data set that
reproduces the problem.) A statement of what you want in the end is
also very helpful.

Dennis

Ace

unread,
Mar 8, 2012, 8:32:38 PM3/8/12
to ggplot2
Sorry, here are a few more details. I have data in a csv file with 3
columns of data. D, A, B loaded. I want to plot

A: scatterplot with D as the x axis and A, B on the Y axis in one
graph with different colors.

B a lineplot with D as the x axis and A, B on the Y axis in one graph
with different colors.

So far I've been able to do 1 x and 1 y column graphs easy but all the
solutions I've seen with 2 dependent variables are confusing.

Dennis Murphy

unread,
Mar 8, 2012, 10:12:20 PM3/8/12
to Ace, ggplot2
Is this more or less what you're after?

library('ggplot2')
library('reshape2')
testdf <- data.frame(x = seq_len(20), y1 = rnorm(20), y2 = rnorm(20, mean = 3))
dfm <- melt(testdf, id = 'x')
ggplot(dfm, aes(x = x, y = value, colour = variable)) +
geom_point() + geom_line(size = 1)

The melt() command from reshape2 is a common way to combine two or
more response variables such that they can be plotted with different
colors, shapes, linesizes, etc. In this case, melt() is basically
taking (x, y1) and (x, y2) and row-concatenating them. The common
variable x is defined as the 'id' variable; y1 and y2 are (implicitly)
defined to be 'measure' variables. dfm contains three variables: x,
variable (a factor with the variable names y1 and y2 as factors) and
value (their corresponding values). In the ggplot() call, value
becomes the y variable and the factor 'variable' is used to map
different colors to different variables (which is why it is inside the
aes() statement).

In the future, you should supply data and code that you've tried (a
fake data example like this is fine) and a statement of what you want
in the end, like you did in this mail. If you're using qplot()
instead, the equivalent code would be

qplot(x, value, data = dfm, colour = variable, geom = 'point') +
geom_line(size = 1)

In ggplot2, you can do a lot of things more easily if you know how to
summarize or reshape your data as necessary. Learning the plyr and
reshape2 packages takes some time (especially the former), but it's
worth the effort.

Dennis

Reply all
Reply to author
Forward
0 new messages