Open up the CSV file

scorpions <- read.csv("newscorp.csv")

Create “species” vector

species <- c(scorpions$species)

Create “vegetation” vector

veg <- c(scorpions$veg_type)

test

library(gmodels)
## Warning: package 'gmodels' was built under R version 4.2.2
with(scorpions, CrossTable(veg,species, header = FALSE))
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  250 
## 
##  
##                   | species 
##               veg | P. silvestrii |   S. gertschi |     U. mordax |     Row Total | 
## ------------------|---------------|---------------|---------------|---------------|
##         chaparral |             3 |            31 |             0 |            34 | 
##                   |         3.738 |         5.160 |         4.624 |               | 
##                   |         0.088 |         0.912 |         0.000 |         0.136 | 
##                   |         0.047 |         0.204 |         0.000 |               | 
##                   |         0.012 |         0.124 |         0.000 |               | 
## ------------------|---------------|---------------|---------------|---------------|
##         grassland |             0 |             6 |             0 |             6 | 
##                   |         1.536 |         1.516 |         0.816 |               | 
##                   |         0.000 |         1.000 |         0.000 |         0.024 | 
##                   |         0.000 |         0.039 |         0.000 |               | 
##                   |         0.000 |         0.024 |         0.000 |               | 
## ------------------|---------------|---------------|---------------|---------------|
##      oak woodland |             4 |             4 |            21 |            29 | 
##                   |         1.579 |        10.539 |        73.759 |               | 
##                   |         0.138 |         0.138 |         0.724 |         0.116 | 
##                   |         0.062 |         0.026 |         0.618 |               | 
##                   |         0.016 |         0.016 |         0.084 |               | 
## ------------------|---------------|---------------|---------------|---------------|
##    open grassland |             0 |             1 |             0 |             1 | 
##                   |         0.256 |         0.253 |         0.136 |               | 
##                   |         0.000 |         1.000 |         0.000 |         0.004 | 
##                   |         0.000 |         0.007 |         0.000 |               | 
##                   |         0.000 |         0.004 |         0.000 |               | 
## ------------------|---------------|---------------|---------------|---------------|
## pine/oak woodland |            45 |            74 |             9 |           128 | 
##                   |         4.566 |         0.188 |         4.061 |               | 
##                   |         0.352 |         0.578 |         0.070 |         0.512 | 
##                   |         0.703 |         0.487 |         0.265 |               | 
##                   |         0.180 |         0.296 |         0.036 |               | 
## ------------------|---------------|---------------|---------------|---------------|
##          riparian |            12 |            36 |             4 |            52 | 
##                   |         0.129 |         0.608 |         1.334 |               | 
##                   |         0.231 |         0.692 |         0.077 |         0.208 | 
##                   |         0.188 |         0.237 |         0.118 |               | 
##                   |         0.048 |         0.144 |         0.016 |               | 
## ------------------|---------------|---------------|---------------|---------------|
##      Column Total |            64 |           152 |            34 |           250 | 
##                   |         0.256 |         0.608 |         0.136 |               | 
## ------------------|---------------|---------------|---------------|---------------|
## 
## 

Create contingency table

library(summarytools)
## Warning: package 'summarytools' was built under R version 4.2.2
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
table <- ctable(species, veg, header = FALSE)
print(table)
## Cross-Tabulation, Row Proportions  
## species * veg  
## 
## --------------- ----- ------------ ----------- -------------- ---------------- ------------------- ------------ --------------
##                   veg    chaparral   grassland   oak woodland   open grassland   pine/oak woodland     riparian          Total
##         species                                                                                                               
##   P. silvestrii          3 ( 4.7%)    0 (0.0%)      4 ( 6.2%)         0 (0.0%)          45 (70.3%)   12 (18.8%)    64 (100.0%)
##     S. gertschi         31 (20.4%)    6 (3.9%)      4 ( 2.6%)         1 (0.7%)          74 (48.7%)   36 (23.7%)   152 (100.0%)
##       U. mordax          0 ( 0.0%)    0 (0.0%)     21 (61.8%)         0 (0.0%)           9 (26.5%)    4 (11.8%)    34 (100.0%)
##           Total         34 (13.6%)    6 (2.4%)     29 (11.6%)         1 (0.4%)         128 (51.2%)   52 (20.8%)   250 (100.0%)
## --------------- ----- ------------ ----------- -------------- ---------------- ------------------- ------------ --------------

Create bar plot

library(ggplot2)
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.2.2
ggplot(data=scorpions, aes(x=species,y=veg,fill=veg))+ geom_bar(stat="identity",color="black",position=position_dodge())+theme_stata()+scale_fill_economist()+ theme(axis.text.y = element_text(angle = 0),axis.title  = element_text(face="bold"))+ labs(title="Relationship Between Species and Vegetation Type")

Run chi-square test

test <- chisq.test(table(species, veg))
## Warning in chisq.test(table(species, veg)): Chi-squared approximation may be
## incorrect
test
## 
##  Pearson's Chi-squared test
## 
## data:  table(species, veg)
## X-squared = 114.8, df = 10, p-value < 2.2e-16

Calculate test statistic

test$statistic
## X-squared 
##  114.7999

Calculate p-value

test$p.value
## [1] 5.724566e-20
library(gmodels)
with(scorpions, CrossTable(species,veg, header = FALSE))
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  250 
## 
##  
##               | veg 
##       species |         chaparral |         grassland |      oak woodland |    open grassland | pine/oak woodland |          riparian |         Row Total | 
## --------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
## P. silvestrii |                 3 |                 0 |                 4 |                 0 |                45 |                12 |                64 | 
##               |             3.738 |             1.536 |             1.579 |             0.256 |             4.566 |             0.129 |                   | 
##               |             0.047 |             0.000 |             0.062 |             0.000 |             0.703 |             0.188 |             0.256 | 
##               |             0.088 |             0.000 |             0.138 |             0.000 |             0.352 |             0.231 |                   | 
##               |             0.012 |             0.000 |             0.016 |             0.000 |             0.180 |             0.048 |                   | 
## --------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
##   S. gertschi |                31 |                 6 |                 4 |                 1 |                74 |                36 |               152 | 
##               |             5.160 |             1.516 |            10.539 |             0.253 |             0.188 |             0.608 |                   | 
##               |             0.204 |             0.039 |             0.026 |             0.007 |             0.487 |             0.237 |             0.608 | 
##               |             0.912 |             1.000 |             0.138 |             1.000 |             0.578 |             0.692 |                   | 
##               |             0.124 |             0.024 |             0.016 |             0.004 |             0.296 |             0.144 |                   | 
## --------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
##     U. mordax |                 0 |                 0 |                21 |                 0 |                 9 |                 4 |                34 | 
##               |             4.624 |             0.816 |            73.759 |             0.136 |             4.061 |             1.334 |                   | 
##               |             0.000 |             0.000 |             0.618 |             0.000 |             0.265 |             0.118 |             0.136 | 
##               |             0.000 |             0.000 |             0.724 |             0.000 |             0.070 |             0.077 |                   | 
##               |             0.000 |             0.000 |             0.084 |             0.000 |             0.036 |             0.016 |                   | 
## --------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
##  Column Total |                34 |                 6 |                29 |                 1 |               128 |                52 |               250 | 
##               |             0.136 |             0.024 |             0.116 |             0.004 |             0.512 |             0.208 |                   | 
## --------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|-------------------|
## 
##