#####Lectura de datos de dulce gloria#####
library(readxl)
datos_dg <- read_excel("dulcegloria.xlsx")

str(datos_dg)
## tibble [796 × 8] (S3: tbl_df/tbl/data.frame)
##  $ registro    : num [1:796] 1 2 3 4 5 6 7 8 9 10 ...
##  $ arbol       : num [1:796] 1 2 3 4 5 6 7 8 9 10 ...
##  $ Especie     : chr [1:796] "Lupuna" "Shihuahuaco" "Tahuari" "Shihuahuaco" ...
##  $ dap (cm)    : num [1:796] 100 70 60 100 70 60 73 70 90 60 ...
##  $ Altura (m)  : num [1:796] 20 16 18 20 20 20 22 21 18 12 ...
##  $ CalidadFuste: num [1:796] 1 2 2 2 2 1 2 2 2 1 ...
##  $ Volumen (m3): num [1:796] 10.21 4 3.31 10.21 5 ...
##  $ Condicion   : chr [1:796] "Aprovechable" "Aprovechable" "Aprovechable" "Aprovechable" ...
datos <- datos_dg

#####codificación de datos#####
library(car)
## Loading required package: carData
datos$CalidadFuste <- factor(datos$CalidadFuste, levels = c("1", "2","3"), 
                             labels = c("Buena", "Regular","Mala"))
datos$Condicion <- factor(datos$Condicion, levels = c("Aprovechable", "Semillero"), 
                          labels = c("Aprovechable", "Semillero"))

#####Generación de tablas de frecuencia#####
#La funcion table crea las frecuencias absolutas
f1 <- table(datos$CalidadFuste)
#La funcion prop.table crea las frecuencias relativas
h1 <- round(prop.table(f1),3) #en proporcion
p1 <- round(prop.table(f1)*100,1) #en porcentaje

##### Creacion de tablas#####
tabla1 <- table(datos$CalidadFuste, datos$Especie)
tabla2 <- table(datos$Condicion, datos$Especie)

##### Grafico de barras #######
# ylim: limites en el eje y
# xlab: nombre del eje x
# ylab: nombre del eje y
# sub: para agregar subt?tulo en la parte inferior
# border: color del borde de las barras

barplot(tabla1, ylim=c(0,200), font.lab=1, font.main=3, cex.main=1, cex.axis=0.6, beside=FALSE, 
        col=c("lightsalmon1", "royalblue", "tomato3"), xlab="Calidad de fuste", ylab="Observaciones",
        main="Clasificacion de calidad de fuste segun especie", sub="Grafico de barras",
        border="black", horiz=FALSE, cex.names=0.5)  
legend("topright", c("Buena", "Regular", "Mala"), 
       fill=c("royalblue", "lightgrey", "tomato3"), cex=0.8, inset=0.06)

barplot(tabla2,ylim=c(0,300),font.lab=1,font.main=3, cex.main=1, cex.axis= 0.6, beside=F, 
        col= c("lightsalmon1","royalblue", "tomato3"),xlab="Calidad de fuste",ylab="Observaciones",
        main="Clasificacion de condicion segun especie",sub="Grafico de barras",
        border="black", horiz = F, cex.names=0.5)
legend("topright", c("Aprovechables", "Semilleros"),
       fill = c("royalblue", "tomato3"), cex = 0.8, inset = 0.06)

datos_dg <- read_excel("dulcegloria.xlsx")

library(car)
datos_dg$CalidadFuste <- factor(datos_dg$CalidadFuste, levels = c("1", "2","3"),
                                labels = c("Buena", "Regular","Mala"))


tabla3 <- table(datos_dg$CalidadFuste,datos_dg$Especie)


barplot(tabla3,xlim=c(0,200),font.lab=2,font.main=2,beside=T, 
        col= c("#E1C4CA","#A57FA0", "#DB8AAE"),xlab="Observaciones",
        ylab="Especies",
        main="Clasificacion de calidad de fuste segun Especie",
        sub="",border="black", horiz = T, cex.names=0.4)
legend("topright", c("Buena", "Regular", "Mala"), 
       fill = c("#E1C4CA","#A57FA0", "#DB8AAE"), cex = 0.8, inset = 0.06)

tabla4 <- table(datos_dg$Condicion,datos_dg$Especie)

barplot(tabla4,xlim=c(0,250),font.lab=2,font.main=2,beside=T, 
        col= c("#E1C4CA","#A57FA0"),xlab="Observaciones",
        ylab="Especies",
        main="Clasificacion de Condicion segun Especie",
        sub="",border="black", horiz = T, cex.names=0.4)
legend("topright", c("Aprovechable", "Semillero"), 
       fill = c("#E1C4CA","#A57FA0"), cex = 0.8, inset = 0.06)