[R] how to plot two variables in a figure using ggplot2

David L Carlson dcarlson at tamu.edu
Wed Dec 16 17:28:17 CET 2015


You can also do this with matplot() which does not require reshaping the data.

Assuming your data is a data frame called dat:

> dput(dat)
structure(list(XX = c(1243L, 2486L, 3729L, 4972L, 6215L, 7458L, 
8701L, 9944L, 11187L), OA = c(0.8157, 0.819, 0.8278, 0.8354, 
0.8475, 0.853, 0.8668, 0.879, 0.899), KA = c(0.779, 0.7829, 0.7934, 
0.8026, 0.814, 0.8224, 0.8301, 0.854, 0.879)), .Names = c("XX", 
"OA", "KA"), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9"))

> matplot(dat$XX, dat[, 2:3], type="l", xlab="XX", ylab="OA & KA")
> legend("topleft", c("OA", "KA"), col=1:2, lty=1:2)

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of John Kane
Sent: Wednesday, December 16, 2015 9:32 AM
To: JimmyGao; r-help mailing list
Subject: Re: [R] how to plot two variables in a figure using ggplot2

Please have a look at
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and/or http://adv-r.had.co.nz/Reproducibility.html for some general suggestions on posting here. In particular have a look at dput() or do a ?dput in R for the best way to supply sample data.

To do the plot you want you will  need to reshape your data from “wide” format to “long” and then plot it with ggplot2

See ?melt in the reshape2 package for how to reshape the data.
Normally you would use the “colour” option to separately plot the two lines so something like should do it.

ggplot(mydata, aes( XX, value, colour = variable)) + some.plot.option.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: jimmygao0204 at 163.com
> Sent: Tue, 15 Dec 2015 21:41:25 +0800 (CST)
> To: r-help at r-project.org
> Subject: [R] how to plot two variables in a figure using ggplot2
> 
> Hi everyone,
> 
> Now,I want to use the following data to produce a figure.But I don't know
> how to do it.Is anyone have some experiences?
> 
> X axis using variable XX and Y axis using variable OA and KA
> 
> XX OA KA
> 
> 1  1243 0.8157 0.7790
> 2  2486 0.8190 0.7829
> 3  3729 0.8278 0.7934
> 4  4972 0.8354 0.8026
> 5  6215 0.8475 0.8140
> 6  7458 0.8530 0.8224
> 7  8701 0.8668 0.8301
> 8  9944 0.8790 0.8540
> 9 11187 0.8990 0.8790
> 
> 
> Thank you very much.
> Jimmy
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if5
Capture screenshots, upload images, edit and send them to your friends
through IMs, post on Twitter®, Facebook®, MySpace™, LinkedIn® – FAST!

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list