I'd like to add the significance letters to a plot using ggeffects
. In my case:
# Packages
library(ggeffects)
library(dplyr)
library(glmmTMB)
library(multcomp)
library(lsmeans)
# My data set
ds <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/temp_ger_ds.csv")
str(ds)
#'data.frame': 140 obs. of 4 variables:
# $ temp : chr "constante" "constante" "constante" "constante" ...
# $ generation : chr "G0" "G0" "G0" "G0" ...
# $ development: int 22 24 22 27 27 24 25 26 27 18 ...
First fit the ziGamma model:
mTCFd <- glmmTMB(development ~ temp * generation, data = ds,
family = ziGamma(link = "log"))
Pairwise Comparison Post Hoc Tests:
lsm.TCFd.temp <- lsmeans(mTCFd, c("temp","generation"))
cld(lsm.TCFd.temp, Letters=letters)
temp generation lsmean SE df lower.CL upper.CL .group
constante G3 3.13 0.0180 129 3.09 3.16 a
constante G2 3.14 0.0180 129 3.11 3.18 ab
constante G0 3.19 0.0191 129 3.15 3.23 abc
constante G1 3.22 0.0180 129 3.18 3.25 bc
constante G4 3.23 0.0185 129 3.19 3.27 cd
flutuante G1 3.32 0.0352 129 3.25 3.39 cde
flutuante G3 3.34 0.0262 129 3.28 3.39 e
flutuante G0 3.36 0.0191 129 3.32 3.39 e
flutuante G4 3.36 0.0393 129 3.28 3.44 def
flutuante G2 3.47 0.0218 129 3.43 3.52 f
Now, display these letters to the plot:
ggpredict(mTCFd, terms = c("temp","generation")) %>% plot(add.data = TRUE)
But if I try:
lt<-cld(lsm.TCFd.temp, Letters=letters)
ggpredict(mTCFd, terms = c("temp","generation")) %>% plot(add.data = TRUE) %>% geom_text(aes(label = lt[,8]), vjust = -0.5)
Error in geom_text(., aes(label = lt[, 8]), vjust = -0.5) :
could not find function "geom_text"
Doesn't work! Please any help with it?
CodePudding user response:
geom_text
is a ggplot function. You may need to set up data for ggplot. Instead of plotting ggpredict
directly, use ggpredict
to get a data.frame.
I generated a new variable x_1
as x axis. You can have your own ways to get this. I just show the rough idea.
ds <- ds %>% mutate(x_1= 1 (readr::parse_number(generation)-2)*0.05 as.integer(temp =="flutuante"),
group = generation)
df_gg <- ggpredict(mTCFd, terms = c("temp","generation")) %>%
mutate(x_1= 1 (readr::parse_number(as.character(group))-2)*0.05 as.integer(x =="flutuante"))
df_gg %>% ggplot(aes(x = x_1, y = predicted, color = group))
geom_jitter(aes(y = development), data = ds, alpha = 0.25)
geom_point()
geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.02)
geom_text(aes(x = x_1, label = lt[, 8]), vjust = -0.5, show.legend = FALSE)
scale_x_continuous(breaks = c(1, 2), labels = c("constante", "flutuante"))