Está en la página 1de 11

JUAN DIEGO PERTUZ MEJIA 2019111022

YOSED GREGORIO DE LA HOZ CARRANZA 2019111051


Asignación 3. Bases de datos y exploración con GGPLOT2.

Taller numero 3
2022

#Librerias requeridas
library(ggplot2)
library(tidyverse)

## Attaching packages tidyverse 1.3.1

## v tibble 3.1.8 v dplyr 1.0.10


## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## v purrr 0.3.4

## Conflicts tidyverse_conflicts()
## x dplyr::filter() masks
stats::filter() ## x dplyr::lag()
masks stats::lag()

library(dplyr)
library(patchwork)

colores<-c("#BCD2EE",
"#A2B5CD",
"#6E7B8B")

#base de datos

maize1<- read.csv2("Maize1.csv" , header = TRUE)


str(maize1)

## ’data.frame’: 807 obs. of 15 variables:


## $ Field_ID : int 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 ...
## $ Year : int 2018 2018 2018 2018 2018 2018 2018 2018 2018 2018 ...
## $ Elev : int 906 906 467 467 623 623 898 898 927 927 ...
## $ System : chr "Hybrid" "Hybrid" "Landrace" "Hybrid" ...
## $ Tillage : chr "No-till" "No-till" "Conventional" "Conventional"
... ## $ Planting : int 176 176 186 186 158 182 162 183 172 172 ...
## $ Nitrogen : num 18 110 110 110 191 191 64 110 170 110 ...
## $ Phosphorus: num 46 46 46 46 46 46 46 46 23 46 ...
## $ Potassium : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Slope : num 1.52 1.52 3.22 3.22 4.5 ...
## $ Clay : int 29 29 44 44 30 30 29 29 29 29 ...
## $ CEC : num 27.6 27.6 32.4 32.4 21.1 ...

1
## $ SOM : num 2.96 2.96 1.02 1.02 1.06 ...
## $ PH : num 7.34 7.34 7.28 7.28 6.36 ...
## $ YIELD : num 3.8 4.45 1.8 3 2.8 3 3.9 4.5 3.8 4.5 ...

2
maizemean<- read.csv2("Maizemean.csv",
header=TRUE) str(maizemean)

## ’data.frame’: 7 obs. of 5 variables:


## $ Year : int 2012 2013 2014 2015 2016 2017 2018
## $ PromedioYIELD : num 4.49 3.6 2.7 3.75 3.11 3.81 3.35
## $ PromedioPhosphorus: num 26 26.9 23.6 44.1 15.9 ...
## $ PromedioNitrogen : num 134.1 100.5 102.6 145.2 98.7 ...
## $ PromedioPotassium : num 6.16 10.3 5.36 30.57 5.45 ...

Histograma

ggplot(maize1, aes(x=YIELD))+ geom_histogram(aes(y=..density..),


colour=1, fill= "white")+
theme_minimal()+ labs(title = "Figura 1", subtitle = " Rendimiento del
Maíz", x= "Rendimiento del maíz",
y="Frecuencia" )+
theme(text=element_text(family ="serif",
size=12))

## ‘stat_bin()‘ using ‘bins = 30‘. Pick better value with ‘binwidth‘.

Figura 1
Rendimiento del Maíz

0.3
Frecuencia

0.2

0.1

0.0

0.0 2.5 5.0 7.5 10.0


Rendimiento del maíz

3
Densidad

ggplot(maize1, aes(x=YIELD, colour=Tillage, fill= Tillage))+


geom_density(alpha=0.5, colour=1)+ scale_fill_manual(values = colores)+ theme_minimal() + labs(title = "Fig
subtitle = " Rendimiento del Maíz en el año 2018", x= "Rendimiento del maíz (Mg ha-1)",
y="Densidad", fill="Práctica de labranza") + theme(text=element_text(family ="serif", size=12),legend.positio

Figura 2
Rendimiento del Maíz en el año 2018

Práctica de labranza Conventional No−till Reduced

0.20

0.15
Densidad

0.10

0.05

0.00

0.0 2.5 5.0 7.5 10.0


Rendimiento del maíz (Mg ha−1)

Área

maizemean %>% mutate_at(c("Year"), as.character())

## Year PromedioYIELD PromedioPhosphorus PromedioNitrogen PromedioPotassium


## 1 2012 4.49 26.02 134.08 6.16
## 2 2013 3.60 26.86 100.54 10.30
## 3 2014 2.70 23.62 102.58 5.36
## 4 2015 3.75 44.08 145.19 30.57
## 5 2016 3.11 15.91 98.71 5.45
## 6 2017 3.81 20.64 101.11 8.14
## 7 2018 3.35 21.62 99.54 7.44

4
ggplot(maizemean, aes(x= Year , y= PromedioYIELD )) + geom_area(colour="#BCD2EE", fill="#6E7B8B",
alpha= 0.25) + theme_minimal() + labs(title = "Figura 3",
x= "Año",
y=" Rendimiento promedio del
maíz (Mg ha-1)") +
theme(text=element_text(family ="serif", size=12))

Figura 3

4
Rendimiento promedio del maíz

3
(Mg ha−1)

2012 2014 2016 2018


Año

Barras

p1<- ggplot(maize1, aes(x=System, y= Nitrogen, fill= System))+


geom_col(alpha=0.8) + scale_fill_manual(values = colores)+
theme_minimal() + labs(subtitle = " Niveles de nitrógeno") +
scale_y_continuous("kg N ha-1") +
scale_x_discrete(NULL)+
theme(text=element_text(family ="serif", size=12), legend.position = "none")

p2<- ggplot(maize1, aes(x=Phosphorus, y= System, fill= System))+


geom_col(alpha=1) + scale_fill_manual(values = colores)+
theme_minimal() + labs(subtitle = " Niveles de fósforo") +
scale_y_discrete(NULL) +
scale_x_continuous("kg P2O5 ha-1") +
theme(text=element_text(family ="serif", size=12), legend.position = "none")

5
columna<- p1+p2
columna + plot_annotation(title = "Figura 4")&
theme(plot.title = element_text( family = "serif", size = 13))

Figura 4

Niveles de nitrógeno Niveles de fósforo

40000 Landrace
kg N ha−1

20000

Hybrid

0
Hybrid Landrace 0 2500 5000 7500 10000 12500
kg P2O5 ha−1

Boxplot

ggplot(maize1, aes(x=System , y=YIELD, fill=System))


+ stat_boxplot(geom = "errorbar",
width = 0.25)+ geom_boxplot(alpha=0.9)+ coord_flip()
+ scale_fill_manual(values = colores)+
theme_minimal() + labs(title= "Figura 5") +
scale_y_continuous("Rendimiento del maíz")
+ scale_x_discrete("Tipo de labranza")+
theme(text=element_text(family ="serif", size=12), legend.position = "none")

6
Figura 5

Landrace
Tipo de labranza

Hybrid

0.0 2.5 5.0 7.5 10.0


Rendimiento del maíz

Violin

ggplot(maize1, aes(x=Tillage , y=YIELD))+ geom_violin(trim = FALSE)+


geom_jitter(position = position_jitter(seed = 1, width = 0.3), alpha=0.20, color= "#A2B5CD")+
stat_summary(fun = "mean",
geom = "point", color = "black", size=3) + theme_minimal()+
labs(title= "Figura 6", x="Tipo de labranza", y="Rendimiento del maíz ")
+ theme(text=element_text(family ="serif", size=12), legend.position =
"left")

7
Figura 6
12

8
Rendimiento del maíz

Conventional No−till Reduced


Tipo de labranza

Regresión

ggplot(maize1, aes(x=YIELD, y= Elev))+ geom_point(alpha=0.5, color="#A2B5CD")


+ geom_smooth(method ="lm", color="black") + labs(title= "Figura 7 ",
x="Rendimiento del Maiz (Mg ha-1)", y="Elevación (m)") +
theme_minimal() + theme(text=element_text(family ="serif", size=12))

## ‘geom_smooth()‘ using formula ’y ~ x’

8
Figura 7

2000

1500
Elevación (m)

1000

500

0.0 2.5 5.0 7.5 10.0


Rendimiento del Maiz (Mg ha−1)
#Pares
Figura 9. Relación y correlación entre variables

library(GGally)

## Registered S3 method overwritten by ’GGally’:


## method from
## +.gg ggplot2

maizeselect<-maize1 %>% select(-Tillage, -Planting, -Year,-Elev,-Field_ID


) maizeselect1<- maizeselect %>% select("YIELD", everything())
str(maizeselect1)

## ’data.frame’: 807 obs. of 10 variables:


## $ YIELD : num 3.8 4.45 1.8 3 2.8 3 3.9 4.5 3.8 4.5 ...
## $ System : chr "Hybrid" "Hybrid" "Landrace" "Hybrid"
... ## $ Nitrogen : num 18 110 110 110 191 191 64 110 170
110 ...
## $ Phosphorus: num 46 46 46 46 46 46 46 46 23 46 ...
## $ Potassium : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Slope : num 1.52 1.52 3.22 3.22 4.5 ...
## $ Clay : int 29 29 44 44 30 30 29 29 29 29 ...
## $ CEC : num 27.6 27.6 32.4 32.4 21.1 ...
## $ SOM : num 2.96 2.96 1.02 1.02 1.06 ...
## $ PH : num 7.34 7.34 7.28 7.28 6.36 ...

9
ggplot2:: theme_set ( ggplot2:: theme_bw ())
ggpairs(maizeselect1, columns = 1:10,
mapping=aes(alpha=0.5),
lower = list(continuous = wrap("points", color = "#3C8200", alpha =
0.25), combo = wrap("box", fill= "#1F733D", alpha = 0.3),
discrete = wrap("facetbar", color = "orange", alpha = 0.3) ),
diag = list(continuous = wrap("densityDiag", fill= "#1F733D", alpha =
0.5)))
YIELD System Nitroge hosphoru otassium Slope Clay CEC SOM PH
0.20 nCorr: Corr:
Corr: Corr: Corr: Corr: Corr: Corr:

D
YIEL
1
0.10
05
5
0.00 .536** .321** .252** 0.382 −0.109 0.119 −0.073 0.055
Hybrid

ystem itroge ospho tassiu Slope Clay


Landrace
300
200 Corr:Corr: Corr: Corr:Corr: Corr:Corr:
100
0 .320**.180** 0.300 −0.01 −0.06 0.127 −0.03
150 Corr: Corr: Corr: Corr: Corr:Corr:
100
50 .291** 0.145 0.040 −0.04 0.0010.003
0
75 Corr: Corr: Corr: Corr:Corr:
50 0.164 0.005 0.004 −0.03 0.059.
25
0
Corr: Corr: Corr:Corr:
30
20 0.066.0.210
10
0 −0.04 −0.108 Corr:Corr:
50 .203** 0.068.
4
30 Corr:
2
10 .711** Corr:Corr:
50 .507**.490**
4
30

CEC
2 Corr:
10
4 .240**
3

SOM
2
1
8
7

PH
6
5
0.02.5.07.150.H0ybLraidndrac0e1020030000 501001500 255075 0 102030 1020304050 1020304050 1 2 3 4 5
6 7 8

#Trivariado

ggplot(maize1, aes(x= SOM, y=YIELD, color= System))+


geom_point(alpha=0.5) + geom_smooth(method = "lm", color="black")
+
scale_color_manual(values = colores)+ theme_minimal()+ labs(title = "Figura
10", x= "Materia orgánica (%)", y= "Rendimiento del maíz (Mg ha-
1)")+
theme(text=element_text(family ="serif", size=12))
+ facet_wrap(~System)
## ‘geom_smooth()‘ using formula ’y ~ x’

10
Figura 10
Hybrid Landrace
10.0
Rendimiento del maíz (Mg ha−1)

7.5

System
5.0 Hybrid
Landrace

2.5

0.0
1 2 3 4 1 2 3 4
Materia orgánica (%)

11

También podría gustarte