1

I need to plot this

glm1 <- glmer(Total.species..MaxN. ~  Bait.Type + (1|Habitat),
              family=poisson, data=df)

But I don't know how to get the fit and se.fit with a categorical variable.

This is the output of my data from dput(df) :

structure(list(Date = c("06/07/2023", "09/07/2023", "29/06/24", 
"29/06/24", "06/07/23", "09/07/23", "20/07/24", "06/07/23", "29/06/24", 
"07/07/23", "10/07/23", "12/07/23", "03/07/23", "12/07/23", "19/07/24", 
"12/30/99", "12/07/23", "19/07/24", "04/07/23", "18/07/24", "20/07/24", 
"04/07/23", "18/07/24", "20/07/24", "08/07/23", "19/07/24", "20/07/24", 
"06/07/23", "14/07/24", "18/07/24", "06/07/23", "14/07/24", "17/07/24", 
"06/07/23", "14/7/24", "17/07/24", "10/07/23", "10/07/23", "10/07/23", 
"13/07/24", "13/07/24", "13/07/24", "20/07/24", "20/07/24"), 
    Site = structure(c(3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
    3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 
    2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 3L, 2L, 
    1L, 3L, 2L, 3L), levels = c("Centre", "East", "West"), class = "factor"), 
    Habitat = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 
    1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 
    1L, 2L, 2L, 1L, 1L), levels = c("Kelp", "Sand"), class = "factor"), 
    Bait.Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L), levels = c("Clams", "Control", "Crabs", 
    "Mackerel", "Worms"), class = "factor"), Time.of.first.arrival = structure(c(6L, 
    5L, 1L, 5L, 4L, 6L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 10L, 4L, 1L, 6L, 1L, 9L, 2L, 1L, 1L, 5L, 1L, 2L, 
    10L, 7L, 3L, 1L, 1L, 3L, 1L, 1L, 2L, 2L, 1L, 8L, 1L, 1L), levels = c("1", 
    "2", "3", "4", "8", "10", "13", "17", "20", "26"), class = "factor"), 
    Maja.brachydactyla = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 
    1L, 2L, 0L, 0L, 0L), Cancer.pagurus = c(0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Homarus.gammarus = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Necora.puber = c(0L, 
    1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Pagurus.sp. = c(0L, 
    2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 1L, 0L, 0L, 0L, 
    0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
    0L, 0L, 0L, 0L, 0L, 3L, 0L, 2L, 0L, 1L, 0L, 0L, 0L), Tritia.reticulata = c(0L, 
    1L, 1L, 0L, 0L, 1L, 2L, 0L, 3L, 2L, 0L, 0L, 30L, 1L, 1L, 
    1L, 0L, 0L, 5L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 22L, 0L, 7L, 0L, 6L, 0L, 0L, 0L), 
    Sepia.officinalis = c(0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 1L), Spondyliosoma.cantharus = c(0L, 1L, 
    0L, 3L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 3L, 5L, 1L, 3L, 
    2L, 0L, 2L, 0L, 0L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 0L, 0L, 
    3L, 1L, 0L, 2L, 0L, 0L, 0L, 4L, 0L, 1L, 1L, 6L), Scyliorhinus.canicula = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Gobiusculus.flavescens = c(0L, 
    0L, 34L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    58L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 72L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
    ), Parablennius.gattorugine = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L), Gobius.sp. = c(0L, 0L, 3L, 0L, 
    0L, 0L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 3L, 0L, 4L, 0L, 
    0L, 1L, 0L, 1L, 0L, 0L, 6L, 10L, 0L, 1L, 0L, 0L, 0L, 0L, 
    31L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Tripterygion.delaisi = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Callionymus.sp. = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 6L, 2L, 0L, 0L, 
    0L, 0L, 0L, 3L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 2L, 
    0L, 0L, 0L, 0L, 0L, 5L, 0L, 2L, 0L, 1L, 0L, 0L, 0L), Labridae = c(0L, 
    0L, 0L, 0L, 0L,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0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), Centrolabrus.exoletus = c(0L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Trisopterus.luscus = c(0L, 
    0L, 3L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 
    0L, 0L, 0L, 1L, 9L, 0L, 1L, 1L, 1L, 1L, 2L, 0L, 8L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 40L), Trisopterus.minutus = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Mustelus.sp. = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Dicentrarchus.labrax = c(2L, 
    0L, 1L, 0L, 0L, 0L, 0L, 3L, 1L, 4L, 2L, 3L, 0L, 3L, 0L, 1L, 
    3L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 2L, 0L, 
    0L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 0L), Mullus.surmuletus = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 1L, 0L, 0L, 
    7L, 2L, 0L, 1L, 2L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 6L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L), Ammodytidae.sp. = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 26L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 127L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 107L, 
    47L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 
    0L), Diplodus.vulgaris = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L), Anemonia.viridis = c(0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Raja.sp. = c(0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Mustelus.asterias = c(0L, 
    0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Conger.conger = c(0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Pollachius.pollachius = c(0L, 
    0L, 2L, 0L, 0L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    2L, 1L, 0L, 2L, 2L, 0L, 0L, 0L, 1L, 1L, 2L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 0L, 0L, 1L, 1L), Chelon.spp. = c(0L, 
    0L, 0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 0L, 3L, 1L, 0L, 5L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Total.species..MaxN. = c(1, 
    4, 7, 3, 2, 3, 7, 4, 5, 4, 2, 4, 4, 12, 6, 8, 5, 8, 3, 8, 
    8, 1, 8, 4, 5, 9, 10, 5, 9, 6, 2, 2, 3, 7, 6, 3, 4, 6, 4, 
    7, 6, 1, 7, 9), Total.individuals..MaxInd. = c(2L, 5L, 45L, 
    6L, 2L, 3L, 9L, 8L, 32L, 11L, 3L, 9L, 39L, 17L, 12L, 67L, 
    16L, 139L, 8L, 14L, 20L, 2L, 15L, 4L, 77L, 16L, 22L, 6L, 
    131L, 55L, 3L, 2L, 5L, 39L, 11L, 6L, 32L, 12L, 12L, 12L, 
    12L, 1L, 7L, 58L)), row.names = c(NA, -44L), class = "data.frame")

I want to plot just Total species x Bait Type but the predict() function doesn't work.

enter image description here

I was plotting habitat but need to take it out.

This usually works but not with a categorical variable.

SpNumGlm <- glm1

NewDataSpNum <- data.frame(Bait.Type=df$Bait.Type,
                           Total.species..MaxN.=df$Total.species..MaxN.)
view(NewDataSpNum)

NewDataSpNumPred<-predict(SpNumGlm, type="response", newdata=NewDataSpNum, re.form=NA, se.fit=TRUE,
                          nsim=100)
view(NewDataSpNumPred)

NewDataSpNum$fit<-NewDataSpNumPred$fit

NewDataSpNum$se.fit<-NewDataSpNumPred$se.fit

NewDataSpNum$Raw <- df$Total.species..MaxN.


(baits <- NewDataSpNum %>% ggplot()+
    geom_bar(aes(x=Bait.Type, fill=Habitat, y=fit),stat="identity", position=position_dodge())+
    
    geom_errorbar(aes(x=Bait.Type, group=Habitat, y=fit, ymin=fit-se.fit, ymax=fit+se.fit),
                  width=0.2, position=position_dodge(0.9), colour = "black")+
    scale_fill_MR("pretty", discrete=TRUE)+
    labs(x="Bait Type",y="Number of Taxa", 
         fill="Habitat")+
    theme_classic()+
    #theme(plot.margin = unit(c(1, 1, 1, 1), "cm"))+
    theme(#legend.position = 'right',legend.box = 'vertical',
      #legend.direction = 'vertical',
      #legend.key=element_rect(fill=NA),
      #legend.text.align = 0.5,legend.title.align = 0,
      axis.title = element_text(size=11.5,vjust=0.5,hjust=0.5),
      axis.text.x = element_text(size = 10.5, colour = "black"),
      axis.text.y = element_text(colour = "black",
                                 size = 10.5),
      axis.line = element_line(color = "black",
                               size = 1, linetype = "solid"),
      #legend.background = element_blank(),   
      #legend.box.background = element_rect(colour = "black"),
      panel.background = element_rect(fill = NA),
      panel.grid.major = element_line(colour = NA),
      panel.grid.minor  = element_line(colour = NA)))
  • 1
    Hi Katie. Welcome to Stack Overflow. It's much harder to help you if you only show us a _picture_ of your data. We can't copy/paste a picture of data into our consoles to run your code. Please make your example _reproducible_ by including the output from `dput(df)`. Otherwise anyone who wanted to help you would have to transcribe your picture by hand, which seems like an unreasonable thing to expect. It should be easy enough to help if you include your data. Thanks. – Allan Cameron Sep 01 '23 at 18:34
  • @AllanCameron Hi, thank you for letting me know. I have added that in, so hopefully should be ok now – Katie OToole Sep 02 '23 at 16:16

0 Answers0