x=rep(1:5,4)
y=rep(1:4,5)
z=sample(rep(c("A", "B", "C","D"), 5))
z
## [1] "C" "C" "D" "D" "B" "D" "C" "A" "B" "A" "A" "D" "B" "C" "B" "A" "A" "B" "C"
## [20] "D"
design = data.frame(x=factor(x), y=factor(y), z = z)
design
## x y z
## 1 1 1 C
## 2 2 2 C
## 3 3 3 D
## 4 4 4 D
## 5 5 1 B
## 6 1 2 D
## 7 2 3 C
## 8 3 4 A
## 9 4 1 B
## 10 5 2 A
## 11 1 3 A
## 12 2 4 D
## 13 3 1 B
## 14 4 2 C
## 15 5 3 B
## 16 1 4 A
## 17 2 1 A
## 18 3 2 B
## 19 4 3 C
## 20 5 4 D
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
ggplot(data=design,
aes(x=x,y=y))+
geom_tile(fill = y,
alpha = 0.2,
col = "blue")+
geom_text(aes(label = z))+
theme_void()
## 2. Randomly generate the yield data following normal distribution # for A (mean = 50, sd = 3), B (mean = 45, sd =2), # C (mean = 40, sd = 5), and D (mean = 52, sd = 2).
yield = seq(30, 60,0.5)
y_A = dnorm(yield, mean = 50, sd = 3)
y_B = dnorm (yield,mean = 45, sd =2)
y_C = dnorm (yield,mean = 40, sd =5)
y_D = dnorm (yield,mean = 52, sd =2)
mydata = data.frame(yield,y_A,y_B,y_C, y_D)
mydata
## yield y_A y_B y_C y_D
## 1 30.0 2.970300e-11 1.217160e-13 1.079819e-02 1.059410e-27
## 2 30.5 8.898522e-11 7.692690e-13 1.312316e-02 1.606209e-26
## 3 31.0 2.592816e-10 4.567360e-12 1.579003e-02 2.287688e-25
## 4 31.5 7.347875e-10 2.547469e-11 1.880982e-02 3.060892e-24
## 5 32.0 2.025294e-09 1.334778e-10 2.218417e-02 3.847299e-23
## 6 32.5 5.429386e-09 6.570009e-10 2.590352e-02 4.542767e-22
## 7 33.0 1.415630e-08 3.037941e-09 2.994549e-02 5.038968e-21
## 8 33.5 3.589920e-08 1.319622e-08 3.427372e-02 5.250725e-20
## 9 34.0 8.854340e-08 5.384880e-08 3.883721e-02 5.139887e-19
## 10 34.5 2.124046e-07 2.064235e-07 4.357044e-02 4.726552e-18
## 11 35.0 4.955732e-07 7.433598e-07 4.839414e-02 4.083118e-17
## 12 35.5 1.124574e-06 2.514754e-06 5.321705e-02 3.313569e-16
## 13 36.0 2.482015e-06 7.991871e-06 5.793831e-02 2.526136e-15
## 14 36.5 5.327914e-06 2.385932e-05 6.245079e-02 1.809147e-14
## 15 37.0 1.112362e-05 6.691511e-05 6.664492e-02 1.217160e-13
## 16 37.5 2.258767e-05 1.762978e-04 7.041307e-02 7.692690e-13
## 17 38.0 4.461008e-05 4.363413e-04 7.365403e-02 4.567360e-12
## 18 38.5 8.569012e-05 1.014524e-03 7.627756e-02 2.547469e-11
## 19 39.0 1.600902e-04 2.215924e-03 7.820854e-02 1.334778e-10
## 20 39.5 2.908942e-04 4.546781e-03 7.939051e-02 6.570009e-10
## 21 40.0 5.140930e-04 8.764150e-03 7.978846e-02 3.037941e-09
## 22 40.5 8.836587e-04 1.586983e-02 7.939051e-02 1.319622e-08
## 23 41.0 1.477283e-03 2.699548e-02 7.820854e-02 5.384880e-08
## 24 41.5 2.402033e-03 4.313866e-02 7.627756e-02 2.064235e-07
## 25 42.0 3.798662e-03 6.475880e-02 7.365403e-02 7.433598e-07
## 26 42.5 5.842767e-03 9.132454e-02 7.041307e-02 2.514754e-06
## 27 43.0 8.740630e-03 1.209854e-01 6.664492e-02 7.991871e-06
## 28 43.5 1.271754e-02 1.505687e-01 6.245079e-02 2.385932e-05
## 29 44.0 1.799699e-02 1.760327e-01 5.793831e-02 6.691511e-05
## 30 44.5 2.477039e-02 1.933341e-01 5.321705e-02 1.762978e-04
## 31 45.0 3.315905e-02 1.994711e-01 4.839414e-02 4.363413e-04
## 32 45.5 4.317253e-02 1.933341e-01 4.357044e-02 1.014524e-03
## 33 46.0 5.467002e-02 1.760327e-01 3.883721e-02 2.215924e-03
## 34 46.5 6.733290e-02 1.505687e-01 3.427372e-02 4.546781e-03
## 35 47.0 8.065691e-02 1.209854e-01 2.994549e-02 8.764150e-03
## 36 47.5 9.397063e-02 9.132454e-02 2.590352e-02 1.586983e-02
## 37 48.0 1.064827e-01 6.475880e-02 2.218417e-02 2.699548e-02
## 38 48.5 1.173551e-01 4.313866e-02 1.880982e-02 4.313866e-02
## 39 49.0 1.257944e-01 2.699548e-02 1.579003e-02 6.475880e-02
## 40 49.5 1.311466e-01 1.586983e-02 1.312316e-02 9.132454e-02
## 41 50.0 1.329808e-01 8.764150e-03 1.079819e-02 1.209854e-01
## 42 50.5 1.311466e-01 4.546781e-03 8.796719e-03 1.505687e-01
## 43 51.0 1.257944e-01 2.215924e-03 7.094919e-03 1.760327e-01
## 44 51.5 1.173551e-01 1.014524e-03 5.665408e-03 1.933341e-01
## 45 52.0 1.064827e-01 4.363413e-04 4.478906e-03 1.994711e-01
## 46 52.5 9.397063e-02 1.762978e-04 3.505660e-03 1.933341e-01
## 47 53.0 8.065691e-02 6.691511e-05 2.716594e-03 1.760327e-01
## 48 53.5 6.733290e-02 2.385932e-05 2.084187e-03 1.505687e-01
## 49 54.0 5.467002e-02 7.991871e-06 1.583090e-03 1.209854e-01
## 50 54.5 4.317253e-02 2.514754e-06 1.190506e-03 9.132454e-02
## 51 55.0 3.315905e-02 7.433598e-07 8.863697e-04 6.475880e-02
## 52 55.5 2.477039e-02 2.064235e-07 6.533638e-04 4.313866e-02
## 53 56.0 1.799699e-02 5.384880e-08 4.768176e-04 2.699548e-02
## 54 56.5 1.271754e-02 1.319622e-08 3.445138e-04 1.586983e-02
## 55 57.0 8.740630e-03 3.037941e-09 2.464438e-04 8.764150e-03
## 56 57.5 5.842767e-03 6.570009e-10 1.745365e-04 4.546781e-03
## 57 58.0 3.798662e-03 1.334778e-10 1.223804e-04 2.215924e-03
## 58 58.5 2.402033e-03 2.547469e-11 8.495605e-05 1.014524e-03
## 59 59.0 1.477283e-03 4.567360e-12 5.838939e-05 4.363413e-04
## 60 59.5 8.836587e-04 7.692690e-13 3.973109e-05 1.762978e-04
## 61 60.0 5.140930e-04 1.217160e-13 2.676605e-05 6.691511e-05
ggplot(data = mydata,
aes(x=yield, y = y_A))+
geom_line(col = "green")+
geom_vline(xintercept = 50,col = "green")+
geom_line(aes(y=y_B),
col="red")+
geom_vline(xintercept=45, col = "red")+
geom_line(aes(y=y_C),
col="blue")+
geom_vline(xintercept=40, col = "blue")+
geom_line(aes(y=y_D),
col="purple")+
geom_vline(xintercept=52, col = "purple")
##
Yield_A = rnorm(5, mean = 50, sd =3)
Yield_B = rnorm(5, mean = 45, sd = 2)
Yield_C = rnorm(5, mean = 40, sd =5)
Yield_D = rnorm(5, mean = 52, sd = 2)
Yield = c(Yield_A, Yield_B,Yield_C, Yield_D)
Yield
## [1] 51.07852 51.12286 56.30921 47.60644 49.32477 44.20488 41.19596 44.88689
## [9] 44.12466 46.05910 42.71667 44.12686 32.53299 46.09552 41.67750 50.77062
## [17] 51.80136 50.69351 50.90091 51.47639
Variety = rep(c("A", "B", "C", "D"), each = 5)
Variety
## [1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" "D"
## [20] "D"
Yield_data = data.frame(Yield,Variety)
Yield_data
## Yield Variety
## 1 51.07852 A
## 2 51.12286 A
## 3 56.30921 A
## 4 47.60644 A
## 5 49.32477 A
## 6 44.20488 B
## 7 41.19596 B
## 8 44.88689 B
## 9 44.12466 B
## 10 46.05910 B
## 11 42.71667 C
## 12 44.12686 C
## 13 32.53299 C
## 14 46.09552 C
## 15 41.67750 C
## 16 50.77062 D
## 17 51.80136 D
## 18 50.69351 D
## 19 50.90091 D
## 20 51.47639 D
summary(Yield_data)
## Yield Variety
## Min. :32.53 Length:20
## 1st Qu.:44.13 Class :character
## Median :46.85 Mode :character
## Mean :46.94
## 3rd Qu.:50.95
## Max. :56.31
mean = with(Yield_data, tapply(Yield, Variety,mean))
mean
## A B C D
## 51.08836 44.09430 41.42991 51.12856
sd = with(Yield_data, tapply(Yield, Variety,sd))
sd
## A B C D
## 3.2593181 1.7959779 5.2419872 0.4855041
res_aov = aov(Yield~Variety, data = Yield_data)
summary(res_aov)
## Df Sum Sq Mean Sq F value Pr(>F)
## Variety 3 366.1 122.02 11.74 0.000257 ***
## Residuals 16 166.3 10.39
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(res_aov)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Yield ~ Variety, data = Yield_data)
##
## $Variety
## diff lwr upr p adj
## B-A -6.99406087 -12.826812 -1.161310 0.0162634
## C-A -9.65845067 -15.491202 -3.825699 0.0011503
## D-A 0.04019764 -5.792554 5.872949 0.9999971
## C-B -2.66438980 -8.497141 3.168361 0.5718975
## D-B 7.03425851 1.201507 12.867010 0.0156306
## D-C 9.69864831 3.865897 15.531399 0.0011058
plot(TukeyHSD(res_aov))
ggplot(Yield_data,
aes(x=Variety,y=Yield), show.legend = T)+
geom_boxplot(aes(fill = Variety))+
scale_fill_manual (values = cm.colors(4))+
ylim(40, 58)+
labs (title = "The differences among varieties") +
theme(plot.title = element_text(family = "TNR",
face = "italic", color = "red", size = 14))+
xlab ("Varieties")+
ylab ("Yield (Ton)")+
theme(axis.title = element_text(family = "TNR",
face = "bold", color = "blue", size =14))+
scale_x_discrete(labels = c("Type A", "Type B", "Type C", "Type D")) +
theme(axis.text = element_text(family = "TNR", face = "bold",
color = "Black", size = 12, angle = 0,
hjust = 1))+
geom_segment(x=3,
y=55,
xend = 3,
yend = 56)+
geom_segment(x=4,
y=56,
xend = 4,
yend = 57)+
geom_segment(x=3,
y=57,
xend = 4,
yend = 57)+
annotate(geom = "text",
x=3.5,
y=57.5,
label = "*")+
annotate(geom = "text",
x=2,
y=57,
label = "P=0.000416")
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database