Side by side scatter plots with specified regression lines

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Side by side scatter plots with specified regression lines

Sigrid
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.
Reply | Threaded
Open this post in threaded view
|

Re: Side by side scatter plots with specified regression lines

William Revelle
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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Side by side scatter plots with specified regression lines

Thierry Onkelinx
In reply to this post by Sigrid
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: [hidden email] [mailto:[hidden email]]
> Namens Sigrid
> Verzonden: zondag 12 juni 2011 21:47
> Aan: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Side by side scatter plots with specified regression lines

Sigrid
Thank you so much for that. I now have this graphs, by using these commands

p=ggplot(data = test, aes(x = YEAR, y = TOTAL, colour = TREATMENT)) + geom_point() + geom_smooth(method = "lm", se=FALSE) + facet_wrap(~COUNTRY)
> p +scale_x_continuous(limits=c(1,4))




I would also like to:


1.) change the headline for the faced wraps ‘high’ and ‘low’. Is there any other way of doing that other than changing the names in the dataset?


2.) show the slope and intercept next to the treatments labels on the right side. The geom_text only works inside the graph. I know I can change the names of the treatments in the data set to include the intercept and slope, but that seems like a detour for what I want to achieve.


3.) avoid the decimals on the x-axis. I would prefer the ticks to be (1,2,3,4). How is that done?


4.) change the lines. The look of the graph is exactly what I am after (except those minor changes mentioned above).  However, the slopes and intercepts are not the same as what I got when I completed an ancova model.

>  coef(lm(formula = TOTAL~ TREATMENT + YEAR + COUNTRY + TREATMENT:YEAR, data= test))
    .... r gives the values for the different intercepts and slopes...

a. I’ve tried adding an abline that specifies slope and intercept. But then I would have to specify which of the facet wraps that the line belongs to, as it turns up in both grids. Is that possible in qqplot2?

> p=ggplot(data = test, aes(x = YEAR, y = TOTAL, colour = TREATMENT)) + geom_point() + facet_wrap(~NITROGEN)

#this is the treatment A, but should only be displayed in the right graph (low).
p + geom_abline(intercept = 81.476,slope=47.2667, colour = "red", size = 1)


 
b. Another option I’ve tried is to specify the model in the stat_smooth and geom_smooth commandsm=, but in  both cases I get an error message.

> p + stat_smooth(method = "lm", formula = TOTAL ~ TREATMENT + YEAR + COUNTRY + TREATMENT:YEAR)+
+ geom_point()

Error in model.frame.default(formula = formula, data = data, weights = weight,  :
  variable lengths differ (found for 'COUNTRY')

Thank you for all help and creative solutions.

Sigrid