Re: [R] how to plot two variables in a figure using ggplot2
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-boun...@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: jimmygao0...@163.com > Sent: Tue, 15 Dec 2015 21:41:25 +0800 (CST) > To: r-help@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@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@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. __ R-help@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.
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: jimmygao0...@163.com > Sent: Tue, 15 Dec 2015 21:41:25 +0800 (CST) > To: r-help@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@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@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.
[R] how to plot two variables in a figure using ggplot2
Greetings! You don't specify what type of "figure" you're looking for...here are two possibilities to get you started: As a note, it would have made things slightly easier if you had used dput() to provide the data to us. It took a little massaging in notepad before getting it into R. #Read data from clipboard after removing blank line and column numbers in notepad... mydata <- read.table(file="clipboard", sep=" ", header =TRUE) str(mydata) #Restructure the data to long for ggplot2...there are other ways to do this, also. mydata2 <- data.frame(group=rep(c("OA","KA"), each=9), xvar=rep(mydata$XX, times=2), yvar=c(mydata$OA, mydata$KA)) #Look at format of the data mydata2 #Now to the plotting... require(ggplot2) #You might want a barplot... plot1 <- ggplot(data=mydata2, aes(x=xvar, y=yvar,group=group, fill=group)) + geom_bar(stat="identity", position="dodge") + ggtitle("Barchart of OA and KA") plot1 #..or you might want a lineplot... plot2 <-ggplot(data=mydata2, aes(x=xvar, y=yvar, group=group, colour=group)) + geom_line() + ggtitle("Linechart of OA and KA") plot2 The line chart is very easy to do in ggplot2 even if you don't restructure the data to long format, if that is what you were looking for. Hope this helps and best regards! Jim Gullen, Ph.D Higher Education Data Reporting Consultant Office of Professional Preparation Services Michigan Department of Education Customer Service is a priority at the Michigan Department of Education - helping Michigan schools, families, and communities improve the achievement and well-being of ALL our children. __ R-help@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.
[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@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.