Sigrid
2011-Jun-12 19:46 UTC
[R] Side by side scatter plots with specified regression lines
I am new and self taught in R, so please bear with me. I want to create two scatter plots side by side. The data set includes measurements from two different countries with 7 treatments over a timeline (x-axis). Problem 1 I want to have each plot to include the data from one of the countries with 7 regression lines of the treatments, but I do no know how to divide the data between them. This is how I created one plot with all the data.> plot(YEAR,YIELD,col="red",xlab="Year",ylab="Yield",xlim=c(1,4),ylim=c(1,150))Problem 2 The models I've found to describe the regression lines of the treatments seems to be different than the default ablines that R creates. I have the values of the exact values of intercepts and slopes, but does not know how to add them to the graph. This is what I got so far.> abline(lm(YIELD[TREATMENT=="A"]~YEAR[TREATMENT=="A"]),lty=2,col="1")I hope this is enough to give me some pointers, otherwise I will try to elaborate. Thank you for your help. -- View this message in context: http://r.789695.n4.nabble.com/Side-by-side-scatter-plots-with-specified-regression-lines-tp3592473p3592473.html Sent from the R help mailing list archive at Nabble.com.
William Revelle
2011-Jun-13 02:53 UTC
[R] Side by side scatter plots with specified regression lines
Dear Sigrid, At 12:46 PM -0700 6/12/11, Sigrid wrote:>I am new and self taught in R, so please bear with me. > >I want to create two scatter plots side by side. The data set includes >measurements from two different countries with 7 treatments over a timeline >(x-axis). > >Problem 1 >I want to have each plot to include the data from one of the countries with >7 regression lines of the treatments, but I do no know how to divide the >data between them. This is how I created one plot with all the data. > >> >>plot(YEAR,YIELD,col="red",xlab="Year",ylab="Yield",xlim=c(1,4),ylim=c(1,150)) > >Problem 2 >The models I've found to describe the regression lines of the treatments >seems to be different than the default ablines that R creates. I have the >values of the exact values of intercepts and slopes, but does not know how >to add them to the graph. This is what I got so far. > >> abline(lm(YIELD[TREATMENT=="A"]~YEAR[TREATMENT=="A"]),lty=2,col="1") > >I hope this is enough to give me some pointers, otherwise I will try to >elaborate. > >Thank you for your help.Here is an example that might help: library(psych) #in order to get the sat.act data set my.data <- sat.act with(my.data,plot(SATV~SATQ, col=c("blue","red")[gender])) by(my.data,my.data$education, function(x) abline (lm(SATV~SATQ,data=x), lty=c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash")[(x$education+1)])) #to make two scatter plots side by side, use op <- par(mfrow=c(1,2)) I hope this helps. Bill> >-- >View this message in context: >http://r.789695.n4.nabble.com/Side-by-side-scatter-plots-with-specified-regression-lines-tp3592473p3592473.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at r-project.org mailing list >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.
ONKELINX, Thierry
2011-Jun-15 07:46 UTC
[R] Side by side scatter plots with specified regression lines
Dear Sigrid,
This is very easy with the ggplot2 package
install.packages("ggplot2")
library(ggplot2)
ggplot(data = Your.Data.Frame, aes(x = YEAR, y = YIELD, colour = TREATMENT)) +
geom_point() + geom_smooth(method = "lm") + facet_wrap(~Country)
Best regards,
Thierry
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org]
> Namens Sigrid
> Verzonden: zondag 12 juni 2011 21:47
> Aan: r-help at r-project.org
> Onderwerp: [R] Side by side scatter plots with specified regression lines
>
> I am new and self taught in R, so please bear with me.
>
> I want to create two scatter plots side by side. The data set includes
> measurements from two different countries with 7 treatments over a timeline
> (x-axis).
>
> Problem 1
> I want to have each plot to include the data from one of the countries with
> 7 regression lines of the treatments, but I do no know how to divide the
data
> between them. This is how I created one plot with all the data.
>
> >
plot(YEAR,YIELD,col="red",xlab="Year",ylab="Yield",xlim=c(1,4),ylim=c(
> > 1,150))
>
> Problem 2
> The models I've found to describe the regression lines of the
treatments seems
> to be different than the default ablines that R creates. I have the values
of the
> exact values of intercepts and slopes, but does not know how to add them to
> the graph. This is what I got so far.
>
> >
abline(lm(YIELD[TREATMENT=="A"]~YEAR[TREATMENT=="A"]),lty=2,col="1")
>
> I hope this is enough to give me some pointers, otherwise I will try to
elaborate.
>
> Thank you for your help.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Side-by-side-
> scatter-plots-with-specified-regression-lines-tp3592473p3592473.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.