user55546
user55546

Reputation: 62

Estimated averages of a glmm by the emmeans package

I am trying to plot the estimated means using a negative binomial random effects model with their respective confidence intervals, in which the data and the fit were defined as follows:

d.AD <- data.frame(treatment = gl(12,12),
                   treatment2 = gl(4,1,36),
                   counts = rpois(144, 4.03),
                   treatment3 = gl(3,4,36),
                   ID = gl(12,12))
d.AD$treatment2 = as.integer(d.AD$treatment2)
d.AD$treatment3 = as.factor(d.AD$treatment3)
d.AD$ID = as.factor(d.AD$ID)

library(ggplot2)
library(lme4)
library(emmeans)

mode1A <- glmer.nb(counts~treatment3+poly(treatment2, 2) + (1|ID), 
                   data=d.AD, verbose=F)

The function to make the averages with the confidence intervals used was the emmeans of the emmeans package. However, see that the shape of the graph is inverted, that is, I would like the x axis to be my y axis and vice versa.

emm1 = emmeans(mode1A,~"treatment3", type="response")
emm1df = as.data.frame(emm1)
plot(emm1) +
  geom_point(size=2.2)+
  labs(x = 'Response', y = 'Treatment') +  
  scale_shape_manual(NULL, values = 18) +
  theme(legend.title = element_text(size = 25,color = "black"),
        legend.text = element_text(size = 25,color = "black"),
        axis.title = element_text(size = 25,color = "black"),
        axis.text.x = element_text(color = "black", hjust=1),
        axis.text.y = element_text(color = "black", hjust=1),
        axis.text = element_text(size = 25,color = "black"),
        plot.title = element_blank(),
        strip.text.x = element_text(size = 15,color = "black"))

enter image description here

I would like something close to this example.

enter image description here

Upvotes: 1

Views: 328

Answers (2)

Russ Lenth
Russ Lenth

Reputation: 6810

Try

plot(emm1, horizontal = FALSE)

See `? plot.emmGrid

Upvotes: 0

harre
harre

Reputation: 7297

You could do something like:

emm1df |>
  ggplot(aes(x     = treatment3,
             y     = response,
             label = round(response,2)
             )
         ) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = asymp.LCL,
                    ymax = asymp.UCL),
                width = 0
                ) +
  geom_label(nudge_y = (emm1df$asymp.UCL-emm1df$response)*1.2) +
  ylim(c(3,5)) +
  labs(x = "Treatment",
       y = "Response")

Example

Upvotes: 1

Related Questions