箱线图的问题:函数创建中的扁平线

Problem with boxplot: flat lines in function creation

我对这个函数有疑问(我是 R 的新手)

我想创建一个简单的函数,在其中插入一个基因(变量)以获得一个简单的箱线图来比较两个条件(条件),但我得到了两条平线,但我不知道为什么。 在函数中,我想在 x 轴上设置“条件”,在 y 轴上设置变量值,并且我从 df 中删除了最初的 na 值,但我认为这不是问题所在。

    funzione <- function(variabile) {
    
    tab <- tab_n %>% filter(!is.na(Condition))
    tab %>% ggplot(aes(x = Condition, y = variabile, fill = Condition)) + 
    geom_boxplot(alpha = 0.5) +
    geom_point(position = position_dodge(width=0.75)) 
    ggpubr::stat_compare_means(aes(group = Condition), label.x.npc = "center", size = 3.2) +
    theme_bw()
}

这是我拥有的数据库的第一行。

Gender Condition variable_to_insert
Male cond_1 5.6
Female cond_1 4.7
Female cond_2 4.8
Female cond_1 5.8
Male cond_2 5.1
Female cond_1 5.5
Male cond_2 7.9
Male cond_2 7.1
Female cond_1 2.9

下面的函数被重写为接受一个 data.frame 和一个变量作为参数。

library(dplyr)
library(ggplot2)

funzione <- function(x, variabile) {
  x %>% 
    filter(!is.na(Condition)) %>%
    ggplot(aes(x = Condition, y = {{variabile}}, fill = Condition)) + 
    geom_boxplot(alpha = 0.5) +
    geom_point(position = position_dodge(width=0.75))  +
    ggpubr::stat_compare_means(aes(group = Condition), label.x.npc = "center", size = 3.2) +
    theme_bw()
}

tab_n %>% funzione(variable_to_insert)

reprex package (v2.0.1)

创建于 2022-02-14

数据

tab_n <- read.table(text = "
Gender  Condition   variable_to_insert
Male    cond_1  5.6
Female  cond_1  4.7
Female  cond_2  4.8
Female  cond_1  5.8
Male    cond_2  5.1
Female  cond_1  5.5
Male    cond_2  7.9
Male    cond_2  7.1
Female  cond_1  2.9
", header = TRUE)

reprex package (v2.0.1)

创建于 2022-02-14