-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression_analysis_with_variables_in_the_same_column
70 lines (59 loc) · 2.83 KB
/
regression_analysis_with_variables_in_the_same_column
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
# data upload
github="https://raw.githubusercontent.com/agronomy4future/raw_data_practice/main/wheat_grain_Fe_uptake.csv"
dataA= data.frame(read_csv(url(github), show_col_types= FALSE))
# to adjust unbalanced data
dataA_filtered= dataA %>%
group_by(Location, Season, Genotype, Stage) %>%
filter(n()>=20) %>%
ungroup()
dummy_rows= dataA_filtered %>%
complete(Location, Season, Genotype, Stage,
fill = list(Iron_ton_ha=NA, Fe=NA))
dataA_combined= bind_rows(dataA_filtered, dummy_rows)
# to transpose data structure
dataB= dataA_filtered %>%
group_by(Location, Season, Genotype, Stage) %>%
summarise(Iron_ton_ha=list(Iron_ton_ha), Fe=list(Fe)) %>%
pivot_wider(names_from=Season, values_from=c(Iron_ton_ha, Fe), names_sep="_") %>%
unnest(everything())
# to summarize data
dataC= data.frame(dataB %>%
group_by(Location, Genotype, Stage) %>%
dplyr::summarize(across(c(Fe_2021, Fe_2022),
.fns= list(Mean=mean,
SD= sd,
n=length,
se=~ sd(.)/sqrt(length(.))))))
# to simplify data
dataC=dataC[, c("Location", "Genotype", "Stage", "Fe_2021_Mean",
"Fe_2021_se", "Fe_2022_Mean", "Fe_2022_se")]
# to create a graph
FIGA=ggplot(data=subset(dataC, Stage=="Maturity"), aes(x=Fe_2021_Mean, y=Fe_2022_Mean)) +
geom_errorbar(aes(xmin=Fe_2021_Mean-Fe_2021_se, xmax=Fe_2021_Mean+Fe_2021_se),
position=position_dodge(0.7),
width=0.01) +
geom_errorbar(aes(ymin=Fe_2022_Mean-Fe_2022_se, ymax=Fe_2022_Mean+Fe_2022_se),
position=position_dodge(0.7),
width=0.01) +
geom_point(aes(fill=Location, shape=Location), color= "black", size = 5) +
geom_smooth(aes(group=Location),method='lm', linetype=1, se=FALSE, color="red", formula=y~x, size=0.5) +
scale_fill_manual(values=c("tomato4","cadetblue")) +
scale_shape_manual(values=c(21,21)) +
scale_x_continuous(breaks=seq(0,0.25,0.05),limits=c(0,0.25)) +
scale_y_continuous(breaks=seq(0,0.1,0.02),limits=c(0,0.1)) +
labs(x="2021 season", y="2022 season") +
theme_classic(base_size=18, base_family="serif") +
theme(legend.position=c(0.8,0.15),
legend.title=element_blank(),
legend.key=element_rect(color="white", fill="white"),
legend.text=element_text(family="serif", face="plain",
size=15, color="black"),
legend.background= element_rect(fill="white"),
strip.background=element_rect(color="white",
linewidth=0.5, linetype="solid"),
axis.line = element_line(linewidth = 0.5, colour="black"))
FIGA+windows(width=5.5, height=5)