R语言 ggeffects:添加的事后测试字母在x位置不匹配

aelbi1ox  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(139)

我想使用ggeffects包将重要性字母添加到图中。

# Packages
library(ggeffects)
library(dplyr)
library(glmmTMB)
library(multcomp)
library(lsmeans)
library(ggplot2)

# 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 ...

通用型号:

mTCFd <- glmmTMB(development ~ temp * generation, data = ds,
               family = ziGamma(link = "log"))

3个组合图:

#1) Plot for temp
    lsm.mTCFd.temp <- lsmeans(mTCFd, c("temp"))
    lt<-cld(lsm.mTCFd.temp, Letters=letters, decreasing = TRUE)
    ds <- ds %>% mutate(x_1=  1+(readr::parse_number(generation)-2)*0.05, 
                        group = generation)
    df_gg <-ggpredict(mTCFd, terms = c("temp"))%>% 
      mutate(x_1=  1+(readr::parse_number(as.character(group))-2)*0.05)
    df_gg %>% plot(add.data = TRUE) + 
      geom_text(aes(x = x_1, label = lt[,7]), vjust = -0.5, show.legend = FALSE) 

#2) Plot for generation
    lsm.mTCFd.gera <- lsmeans(mTCFd, c("generation"))
    lt<-cld(lsm.mTCFd.gera, Letters=letters, decreasing = TRUE)
    ds <- ds %>% mutate(x_1=  1+(readr::parse_number(generation)-2)*0.05, 
                        group = generation)
    df_gg <-ggpredict(mTCFd, terms = c("generation"))%>% 
      mutate(x_1=  1+(readr::parse_number(as.character(group))-2)*0.05)
    df_gg %>% plot(add.data = TRUE) + 
      geom_text(aes(x = x_1, label = lt[,7]), vjust = -0.5, show.legend = FALSE) 

#3) Plot for temp and generation interaction

    lsm.mTCFd.temp.gera <- lsmeans(mTCFd, c("temp","generation"))
    lt<-cld(lsm.mTCFd.temp.gera , Letters=letters, decreasing = TRUE)
    ds <- ds %>% mutate(x_1=  1+(readr::parse_number(generation)-2)*0.05, 
                        group = generation)
    df_gg <-ggpredict(mTCFd, terms = c("temp","generation"))%>% 
      mutate(x_1=  1+(readr::parse_number(as.character(group))-2)*0.05)
    df_gg %>% plot(add.data = TRUE) + 
      geom_text(aes(x = x_1, label = lt[,8]), vjust = -0.5, show.legend = FALSE)

但我总是作为输出:

Raw data not available.
Error in if (attr(x, "logistic", exact = TRUE) == "1" && attr(x, "is.trial",  : 
  missing value where TRUE/FALSE needed

拜托,能帮上忙吗?

omhiaaxx

omhiaaxx1#

下面是一个reprex,它以您和@丹尼尔的代码为基础,只是将字母移动到您想要的位置。我只是将{lsmean}替换为它的后继{emmeans},并对geom_text()参数进行了一些编辑-因此,我没有仔细查看实际完成的操作。
但是,我建议你阅读我对过去here中类似问题的回答,以及关于here专门处理双向交互emmeans的更多细节,这两篇文章也会向你指出this summary on compact letter displays,我希望这对你有所帮助。

# Packages
library(ggeffects)
library(dplyr)
library(glmmTMB)
library(multcomp)
library(emmeans)
library(ggplot2)

# My data set
ds <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/temp_ger_ds.csv")

# General Model
mTCFd <- glmmTMB(development ~ temp * generation,
                 data = ds,
                 family = ziGamma(link = "log"))

lsm.mTCFd.temp <- emmeans(mTCFd, c("temp"))
#> NOTE: Results may be misleading due to involvement in interactions
lt <- cld(lsm.mTCFd.temp, Letters = letters, decreasing = TRUE)
ds <- ds %>% 
  mutate(x_1 =  1 + (readr::parse_number(generation) - 2) * 0.05, group = generation)

# new lines, replace "mutate()" here
df_gg <- ggpredict(mTCFd, terms = c("temp"))
df_gg$x_1 <-
  1 + (readr::parse_number(as.character(df_gg$group)) - 2) * 0.05

df_gg %>% 
  plot(add.data = TRUE) +
  geom_text(aes(x = x, label = lt[, 7]),
            position = position_nudge(x = 0.1),
            show.legend = FALSE)

reprex package(v2.0.1)于2022年8月8日创建

inkz8wg9

inkz8wg92#

我认为不起作用的代码是mutate(),它从数据框中删除了所有属性。当你替换包含mutate()的代码行时,这个代码对我很有效:

lsm.mTCFd.temp <- lsmeans(mTCFd, c("temp"))
lt<-cld(lsm.mTCFd.temp, Letters=letters, decreasing = TRUE)
ds <- ds %>% mutate(x_1=  1+(readr::parse_number(generation)-2)*0.05, 
                    group = generation)

# new lines, replace "mutate()" here
df_gg <-ggpredict(mTCFd, terms = c("temp"))
df_gg$x_1 <- 1+(readr::parse_number(as.character(df_gg$group))-2)*0.05

df_gg %>% plot(add.data = TRUE) + 
  geom_text(aes(x = x_1, label = lt[,7]), vjust = -0.5, show.legend = FALSE)

相关问题