if(!require(googlesheets4)){install.packages("googlesheets4")}
if(!require(googledrive)){install.packages("googledrive")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(DescTools)){install.packages("DescTools")}
#
library(googlesheets4)
library(googledrive)
library(ggplot2)
library(DescTools)

Table Dog Death Cause

Datos en el G-sheets adjunto link

ss= "https://docs.google.com/spreadsheets/d/1A0Leo-pYgqgBuGgHLdpO1HW_uEgz1LjC8EVilgwrzGY/edit?usp=sharing"
hoja= "Hoja2"
rango = "A6:D12"
#
gs4_deauth()
####
dd <- read_sheet(ss,
                  sheet= hoja,
                  range= rango,
                  col_names= TRUE)
dd$total <- dd$animInter+dd$disease+dd$human
head(dd)
## # A tibble: 6 x 5
##   comm  animInter disease human total
##   <chr>     <dbl>   <dbl> <dbl> <dbl>
## 1 CA            7       9     4    20
## 2 CAP           7       9    28    44
## 3 ePo           2      11    11    24
## 4 PC            2      10     4    16
## 5 UA           15      18    17    50
## 6 Zo            5      19     5    29
p <- ggplot(aes(x=comm, y= (human/total), fill=comm), data=dd)
p + geom_col()

#dd$other <- dd$disease + dd$human 
head(dd)
## # A tibble: 6 x 5
##   comm  animInter disease human total
##   <chr>     <dbl>   <dbl> <dbl> <dbl>
## 1 CA            7       9     4    20
## 2 CAP           7       9    28    44
## 3 ePo           2      11    11    24
## 4 PC            2      10     4    16
## 5 UA           15      18    17    50
## 6 Zo            5      19     5    29
obs <- cbind(dd$animInter, dd$disease, dd$human)
chi2 <- chisq.test(obs)
## Warning in chisq.test(obs): Chi-squared approximation may be incorrect
chi2
## 
##  Pearson's Chi-squared test
## 
## data:  obs
## X-squared = 31.96, df = 10, p-value = 0.0004066
#dd[,c(1,2,7)]
chi2$stdres
##            [,1]       [,2]       [,3]
## [1,]  1.6629480  0.3336735 -1.7310706
## [2,] -0.9111561 -3.2551797  4.0723522
## [3,] -1.6107873  0.4589702  0.8814803
## [4,] -0.8531922  1.7818741 -1.0976738
## [5,]  1.8884071 -0.9308218 -0.6340704
## [6,] -0.5099552  2.8575636 -2.4786719
p <- ggplot(aes(x=comm, y= human/total, fill=comm), data=dd)
p + geom_col() + 
  geom_hline(yintercept = 0.38, lty="dashed", lwd=1) +
  geom_text(x=1.7, y=0.4, label= "", col="dark green")

chi2$stdres
##            [,1]       [,2]       [,3]
## [1,]  1.6629480  0.3336735 -1.7310706
## [2,] -0.9111561 -3.2551797  4.0723522
## [3,] -1.6107873  0.4589702  0.8814803
## [4,] -0.8531922  1.7818741 -1.0976738
## [5,]  1.8884071 -0.9308218 -0.6340704
## [6,] -0.5099552  2.8575636 -2.4786719