I want to display three linear regressions, with the R2 value, at the bottom of my plot to show the correlation between O2 and bias for each concentration of C3H8.
C3H8 O2 bias
1 85 20.90 0.01
2 50 20.90 0.10
3 25 20.94 0.32
4 85 10.00 -1.22
5 50 10.00 -1.05
6 25 10.00 -1.29
7 85 0.10 -3.13
8 50 0.10 -2.39
9 25 0.10 -2.55
Here is the code I am using for ggplot:
library(ggrepel)
ggplot(Data.Frame.1, aes(O2, bias)) +
theme_bw() +
theme(legend.position = 'bottom', plot.title = element_text(hjust=0.5)) +
geom_point(aes(colour = factor(C3H8))) +
geom_line(aes(colour = factor(C3H8))) +
geom_text_repel(aes(label=paste(bias),
hjust= 0.4,
vjust=-.8, colour = factor(C3H8)),
size = 3) +
ggtitle(expression(O[2]~Bias)) +
labs(
x = expression('O'[2]),
y = "% bias",
colour = expression('C'[3]*'H'[8]~(ppm))
)
If it doesn't make sense to include the linear regressions in the plot, I would be fine with having them listed as a separate table or data frame. Or even something similar to this:
https://cran.r-project.org/web/packages/jtools/vignettes/summ.html
You could something similar to what you want with next code. For models you can use broom
and for the final plot patchwork
:
library(ggrepel)
library(broom)
library(gridExtra)
library(patchwork)
library(ggplot2)
#Data
df <- structure(list(C3H8 = c(85L, 50L, 25L, 85L, 50L, 25L, 85L, 50L,
25L), O2 = c(20.9, 20.9, 20.94, 10, 10, 10, 0.1, 0.1, 0.1), bias = c(0.01,
0.1, 0.32, -1.22, -1.05, -1.29, -3.13, -2.39, -2.55)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9"))
The code:
#First plot
G1 <- ggplot(Data.Frame.1, aes(O2, bias)) +
theme_bw() +
theme(legend.position = 'bottom', plot.title = element_text(hjust=0.5)) +
geom_point(aes(colour = factor(C3H8))) +
geom_line(aes(colour = factor(C3H8))) +
geom_text_repel(aes(label=paste(bias),
hjust= 0.4,
vjust=-.8, colour = factor(C3H8)),
size = 3) +
ggtitle(expression(O[2]~Bias)) +
labs(
x = expression('O'[2]),
y = "% bias",
colour = expression('C'[3]*'H'[8]~(ppm))
)
#Models
regs <- df %>% group_by(C3H8) %>%
do(fit = lm(bias ~ O2 , data = .))
coeffs <- tidy(regs,fit)
#Format
coeffs[,-c(1,2)] <- round(coeffs[,-c(1,2)],3)
#Arrange new plot
G2 <- G1 / tableGrob(coeffs)
The output:
I am not sure if the relationship you want in lm()
is bias ~ O2
but you can change that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.