[R] Adding regression line to each individual plot in a window with multiple plots
Thierry Onkelinx
thierry.onkelinx at inbo.be
Wed Jan 18 09:46:14 CET 2017
Hi Jake,
You could consider switching to ggplot2
# create a dummy dataset
dataset <- data.frame(
XA = rnorm(100),
XB = rnorm(100, mean = 10),
YA = rnorm(100),
YB = rnorm(100, mean = -10)
)
# convert it to long format
library(tidyr)
long <- dataset %>%
gather("Xcat", "Xvalue", XA:XB) %>%
gather("Ycat", "Yvalue", YA:YB)
# create the plot
library(ggplot2)
ggplot(long, aes(x = Xvalue, y = Yvalue)) +
geom_smooth(method = "lm") +
geom_point() +
facet_grid(Ycat ~ Xcat, scales = "free")
Best regards,
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
2017-01-18 2:04 GMT+01:00 Jake William Andrae <jake.andrae op adelaide.edu.au>:
> 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 op 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list