[R] Adding regression line to each individual plot in a window with multiple plots

Jim Lemon drjimlemon at gmail.com
Wed Jan 18 04:10:11 CET 2017


Hi Jake,
In the second line of your script, you set xpd=NA. That means that
abline will draw a line across the entire display region rather than
restricting it to the plot region. If you must set this at the
beginning, then add these lines:

par(xpd=TRUE)
abline(lm(...))
par(xpd=NA)

for each regression line you display.

Jim


On Wed, Jan 18, 2017 at 1:41 PM, Jake William Andrae
<jake.andrae at adelaide.edu.au> wrote:
> Hi Jim, that works but this is what it then looks like.
>
> -----Original Message-----
> From: Jim Lemon [mailto:drjimlemon at gmail.com]
> Sent: Wednesday, 18 January 2017 1:06 PM
> To: Jake William Andrae <jake.andrae at adelaide.edu.au>
> Cc: r-help at r-project.org
> Subject: Re: [R] Adding regression line to each individual plot in a window with multiple plots
>
> Hi Jake,
> As I don't have your data set, try this:
>
> attach(mtcars)
> plot(mpg~disp,xaxs="i",yaxs="i")
> abline(lm(mpg~disp))
>
> Jim
>
> On Wed, Jan 18, 2017 at 12:04 PM, Jake William Andrae <jake.andrae at adelaide.edu.au> wrote:
>> Hi Everyone,
>>
>>
>>
>> I've constructed a script that adds multiple plots to the plot window, but I'm having a bit of trouble adding a regression line to each individual plot. Of course, the regression lines will vary depending on the variables plotted against one another. I've attached the script.
>>
>>
>> #Growing season
>> #Construction of plots
>> # 12 figures arranged in 3 rows and 4 columns
>> attach(mtcars)
>> par(mfrow=c(3,4), mar = c(.6,.5,.6,1), oma = c(15,4,2,2), xpd = NA)
>> #Concentration plot(Growing_season_precipitation, Concentration, xaxs
>> = "i", yaxs = "i", xlim = c(0, 700), ylim = c(0,500), xlab = NA, ylab
>> = "Concentration", xaxt='n', pch=21,  bg='black',
>> abline(lm(Growing_season_precipitation~Concentration)))
>> plot(Growing_season_VPD, Concentration, xaxs = "i", yaxs = "i", xlim =
>> c(0.6, 1.8), ylim = c(0,500), xlab = NA, ylab = "", xaxt='n', pch=21,
>> bg='black', yaxt='n') plot(Growing_season_Rhmax, Concentration, xaxs =
>> "i", yaxs = "i", xlim = c(35, 60), ylim = c(0,500), xlab = NA, ylab =
>> "", xaxt='n', pch=21,  bg='black', yaxt='n')
>> plot(Growing_season_temperature, Concentration, xaxs = "i", yaxs =
>> "i", xlim = c(20,34), ylim = c(0,500), xlab = NA, ylab = "", xaxt='n',
>> pch=21,  bg='black', yaxt='n') #ACL plot(Growing_season_precipitation,
>> ACLTotal, xaxs = "i", yaxs = "i", xlim = c(0, 700), ylim = c(28,32),
>> xlab = "", ylab = "ACL", xaxt='n',  pch=21,  bg='black')
>> plot(Growing_season_VPD, ACLTotal, xaxs = "i", yaxs = "i", xlim =
>> c(0.6, 1.8), ylim = c(28,32), xlab = "", ylab = "", xaxt='n',  pch=21,
>> bg='black', yaxt='n') plot(Growing_season_Rhmax, ACLTotal, xaxs = "i",
>> yaxs = "i", xlim = c(35, 60), ylim = c(28,32), xlab = "", ylab = "",
>> xaxt='n',  pch=21,  bg='black', yaxt='n')
>> plot(Growing_season_temperature, ACLTotal, xaxs = "i", yaxs = "i",
>> xlim = c(20,34), ylim = c(28,32), xlab = "", ylab = "", xaxt='n',
>> pch=21,  bg='black', yaxt='n') #CPI plot(Growing_season_precipitation,
>> CPITotal, xaxs = "i", yaxs = "i", xlim = c(0, 700), ylim = c(0,30),
>> xlab = "Total precipitation (mm)", ylab = "CPI",  pch=21,  bg='black')
>> plot(Growing_season_VPD, CPITotal, xaxs = "i", yaxs = "i", xlim =
>> c(0.6, 1.8), ylim = c(0,30), xlab = "Average daily VPD (kpa)", ylab =
>> "",  pch=21,  bg='black', yaxt='n') plot(Growing_season_Rhmax,
>> CPITotal, xaxs = "i", yaxs = "i", xlim = c(35, 60), ylim = c(0,30),
>> xlab = "Average daily RHmax (%)", ylab = "",  pch=21,  bg='black',
>> yaxt='n') plot(Growing_season_temperature, CPITotal, xaxs = "i", yaxs
>> = "i", xlim = c(20,34), ylim = c(0,30), xlab = "Average daily
>> temperature (oC)", ylab = "",  pch=21,  bg='black', yaxt='n') #Plot
>> main title title(main="Growing season (June-November,
>> inclusive)",outer=T)
>>
>> Any help would be greatly appreciated!
>>
>>
>>
>>         [[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.



More information about the R-help mailing list