library(plyr)
## Warning: package 'plyr' was built under R version 4.1.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.2
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode

R script for Figure 4A

setwd("~/Desktop")

tab <- read.csv("~/Desktop/Fig4A1.csv")

str(tab)
## 'data.frame':    240 obs. of  4 variables:
##  $ Concentration: num  0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 ...
##  $ Compound     : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ Group        : int  0 0 0 0 0 1 1 1 1 1 ...
##  $ Number       : int  0 1 2 0 2 0 0 2 2 1 ...
tab$Compound <- as.factor(tab$Compound)
tab$Group <-as.factor(tab$Group)
tab$Concentration <- as.factor(tab$Concentration)

hist(tab$Number)

tab$Compound <-   revalue(tab$Compound, 
                           c( "1"="Isocarophyllene",
                              "2"="Menthone",
                              "3"="Anethole",
                              "4"="Cineole",
                              "5"="Carvone",
                              "6"="Decadiene"))

tab$Group <- revalue(tab$Group, c("0"="Blank", "1"="Pure compound"))

Create figure

names(tab)
## [1] "Concentration" "Compound"      "Group"         "Number"
tabm<-ddply(tab, c("Compound", "Concentration", "Group"), summarise,
               mean = mean(Number), sd = sd(Number),
               sem = sd(Number)/sqrt(length(Number)))


value_max = tabm %>% 
  dplyr::group_by(Concentration) %>% 
  dplyr::summarize(max_value = max(mean), sd = sd(mean))

tabm$Group
##  [1] Blank         Pure compound Blank         Pure compound Blank        
##  [6] Pure compound Blank         Pure compound Blank         Pure compound
## [11] Blank         Pure compound Blank         Pure compound Blank        
## [16] Pure compound Blank         Pure compound Blank         Pure compound
## [21] Blank         Pure compound Blank         Pure compound Blank        
## [26] Pure compound Blank         Pure compound Blank         Pure compound
## [31] Blank         Pure compound Blank         Pure compound Blank        
## [36] Pure compound Blank         Pure compound Blank         Pure compound
## [41] Blank         Pure compound Blank         Pure compound Blank        
## [46] Pure compound Blank         Pure compound
## Levels: Blank Pure compound
tabm <- tabm %>%
  mutate(mean = ifelse(Group == "Pure compound",
                            mean,  -1*mean))

##


fig4a<-ggplot(tabm, aes(x=Concentration, y=mean, fill=Group)) + 
  geom_bar( stat="identity") +
  geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem),
                width=.2, color = "grey20")+
  coord_flip() +
  theme_bw()+
  scale_y_continuous(labels = abs)+
  facet_grid(rows = vars(Compound)) + theme(legend.position = "top")+
  xlab("Concentration (mg/l)") +
  ylab("Number of nematodes") +
  scale_fill_brewer(palette="Blues")+
  theme(axis.text = element_text(size = 10))+
  theme(axis.title = element_text(size = 12))  


fig4a + facet_wrap(~ Compound, ncol=2)

names(tab)
## [1] "Concentration" "Compound"      "Group"         "Number"
hist(tab$Number)

fit<-glm(Number~Concentration*Compound*Group, data = tab, family = "quasipoisson")
summary(fit)
## 
## Call:
## glm(formula = Number ~ Concentration * Compound * Group, family = "quasipoisson", 
##     data = tab)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.3664  -1.0955  -0.3257   0.6734   2.2038  
## 
## Coefficients:
##                                                          Estimate Std. Error
## (Intercept)                                            -9.287e-15  5.108e-01
## Concentration0.01                                       7.885e-01  6.161e-01
## Concentration0.1                                        1.030e+00  5.951e-01
## Concentration1                                          1.386e+00  5.711e-01
## CompoundMenthone                                        9.555e-01  6.011e-01
## CompoundAnethole                                        3.365e-01  6.688e-01
## CompoundCineole                                         1.224e+00  5.811e-01
## CompoundCarvone                                         3.365e-01  6.688e-01
## CompoundDecadiene                                       4.700e-01  6.512e-01
## GroupPure compound                                      2.600e-15  7.224e-01
## Concentration0.01:CompoundMenthone                     -7.143e-01  7.570e-01
## Concentration0.1:CompoundMenthone                      -1.030e+00  7.449e-01
## Concentration1:CompoundMenthone                        -1.061e+00  7.064e-01
## Concentration0.01:CompoundAnethole                     -2.495e-01  8.214e-01
## Concentration0.1:CompoundAnethole                      -4.906e-01  8.057e-01
## Concentration1:CompoundAnethole                        -2.412e-01  7.562e-01
## Concentration0.01:CompoundCineole                      -4.436e-01  7.146e-01
## Concentration0.1:CompoundCineole                       -1.224e+00  7.239e-01
## Concentration1:CompoundCineole                         -1.329e+00  6.895e-01
## Concentration0.01:CompoundCarvone                      -4.318e-01  8.345e-01
## Concentration0.1:CompoundCarvone                       -8.516e-02  7.829e-01
## Concentration1:CompoundCarvone                         -6.931e-01  7.783e-01
## Concentration0.01:CompoundDecadiene                    -2.288e-01  7.974e-01
## Concentration0.1:CompoundDecadiene                     -1.133e-01  7.632e-01
## Concentration1:CompoundDecadiene                       -5.754e-01  7.495e-01
## Concentration0.01:GroupPure compound                   -1.299e+00  1.037e+00
## Concentration0.1:GroupPure compound                    -8.473e-01  9.124e-01
## Concentration1:GroupPure compound                      -1.897e+00  1.011e+00
## CompoundMenthone:GroupPure compound                    -2.565e+00  1.388e+00
## CompoundAnethole:GroupPure compound                    -8.473e-01  1.069e+00
## CompoundCineole:GroupPure compound                     -1.735e+00  1.017e+00
## CompoundCarvone:GroupPure compound                     -8.473e-01  1.069e+00
## CompoundDecadiene:GroupPure compound                   -1.677e+01  1.074e+03
## Concentration0.01:CompoundMenthone:GroupPure compound   2.324e+00  1.734e+00
## Concentration0.1:CompoundMenthone:GroupPure compound    1.946e+00  1.665e+00
## Concentration1:CompoundMenthone:GroupPure compound      2.265e+00  1.775e+00
## Concentration0.01:CompoundAnethole:GroupPure compound   3.548e-01  1.568e+00
## Concentration0.1:CompoundAnethole:GroupPure compound    8.191e-01  1.350e+00
## Concentration1:CompoundAnethole:GroupPure compound     -3.466e-01  1.734e+00
## Concentration0.01:CompoundCineole:GroupPure compound    9.544e-01  1.441e+00
## Concentration0.1:CompoundCineole:GroupPure compound     1.041e+00  1.368e+00
## Concentration1:CompoundCineole:GroupPure compound       2.128e+00  1.390e+00
## Concentration0.01:CompoundCarvone:GroupPure compound    9.426e-01  1.504e+00
## Concentration0.1:CompoundCarvone:GroupPure compound     1.905e-01  1.361e+00
## Concentration1:CompoundCarvone:GroupPure compound       7.985e-01  1.546e+00
## Concentration0.01:CompoundDecadiene:GroupPure compound  1.682e+01  1.074e+03
## Concentration0.1:CompoundDecadiene:GroupPure compound   1.572e+01  1.074e+03
## Concentration1:CompoundDecadiene:GroupPure compound     1.578e+01  1.074e+03
##                                                        t value Pr(>|t|)  
## (Intercept)                                              0.000   1.0000  
## Concentration0.01                                        1.280   0.2021  
## Concentration0.1                                         1.730   0.0852 .
## Concentration1                                           2.427   0.0161 *
## CompoundMenthone                                         1.590   0.1135  
## CompoundAnethole                                         0.503   0.6155  
## CompoundCineole                                          2.106   0.0365 *
## CompoundCarvone                                          0.503   0.6155  
## CompoundDecadiene                                        0.722   0.4713  
## GroupPure compound                                       0.000   1.0000  
## Concentration0.01:CompoundMenthone                      -0.944   0.3465  
## Concentration0.1:CompoundMenthone                       -1.382   0.1685  
## Concentration1:CompoundMenthone                         -1.502   0.1348  
## Concentration0.01:CompoundAnethole                      -0.304   0.7617  
## Concentration0.1:CompoundAnethole                       -0.609   0.5433  
## Concentration1:CompoundAnethole                         -0.319   0.7501  
## Concentration0.01:CompoundCineole                       -0.621   0.5355  
## Concentration0.1:CompoundCineole                        -1.691   0.0926 .
## Concentration1:CompoundCineole                          -1.928   0.0554 .
## Concentration0.01:CompoundCarvone                       -0.517   0.6055  
## Concentration0.1:CompoundCarvone                        -0.109   0.9135  
## Concentration1:CompoundCarvone                          -0.891   0.3742  
## Concentration0.01:CompoundDecadiene                     -0.287   0.7744  
## Concentration0.1:CompoundDecadiene                      -0.148   0.8821  
## Concentration1:CompoundDecadiene                        -0.768   0.4436  
## Concentration0.01:GroupPure compound                    -1.253   0.2117  
## Concentration0.1:GroupPure compound                     -0.929   0.3542  
## Concentration1:GroupPure compound                       -1.877   0.0621 .
## CompoundMenthone:GroupPure compound                     -1.848   0.0662 .
## CompoundAnethole:GroupPure compound                     -0.792   0.4291  
## CompoundCineole:GroupPure compound                      -1.706   0.0896 .
## CompoundCarvone:GroupPure compound                      -0.792   0.4291  
## CompoundDecadiene:GroupPure compound                    -0.016   0.9876  
## Concentration0.01:CompoundMenthone:GroupPure compound    1.340   0.1819  
## Concentration0.1:CompoundMenthone:GroupPure compound     1.169   0.2440  
## Concentration1:CompoundMenthone:GroupPure compound       1.276   0.2036  
## Concentration0.01:CompoundAnethole:GroupPure compound    0.226   0.8212  
## Concentration0.1:CompoundAnethole:GroupPure compound     0.607   0.5448  
## Concentration1:CompoundAnethole:GroupPure compound      -0.200   0.8418  
## Concentration0.01:CompoundCineole:GroupPure compound     0.662   0.5085  
## Concentration0.1:CompoundCineole:GroupPure compound      0.761   0.4475  
## Concentration1:CompoundCineole:GroupPure compound        1.531   0.1275  
## Concentration0.01:CompoundCarvone:GroupPure compound     0.627   0.5316  
## Concentration0.1:CompoundCarvone:GroupPure compound      0.140   0.8888  
## Concentration1:CompoundCarvone:GroupPure compound        0.517   0.6060  
## Concentration0.01:CompoundDecadiene:GroupPure compound   0.016   0.9875  
## Concentration0.1:CompoundDecadiene:GroupPure compound    0.015   0.9883  
## Concentration1:CompoundDecadiene:GroupPure compound      0.015   0.9883  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for quasipoisson family taken to be 1.304616)
## 
##     Null deviance: 532.90  on 239  degrees of freedom
## Residual deviance: 277.34  on 192  degrees of freedom
## AIC: NA
## 
## Number of Fisher Scoring iterations: 14
Anova(fit)
## Analysis of Deviance Table (Type II tests)
## 
## Response: Number
##                              LR Chisq Df Pr(>Chisq)    
## Concentration                  11.833  3   0.007978 ** 
## Compound                        4.099  5   0.535248    
## Group                         150.730  1  < 2.2e-16 ***
## Concentration:Compound         10.497 15   0.787397    
## Concentration:Group             4.883  3   0.180577    
## Compound:Group                  4.618  5   0.464205    
## Concentration:Compound:Group    9.891 15   0.826546    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.