zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

只有四个数据,但图上出现五个点?

2023-03-07 09:47:03 时间

数据为真实数据修改后的数据。

很早之前给师姐调试了一个适配她大批量实验的作图代码,最近她发现有些问题,部分组数据只有四个,但图中却出现了五个点!

使用代码如下

rm(list = ls())
data = read.csv(file = "test.txt",header = T,sep = "\t")
library(tidyr)
ldata = pivot_longer(data = data,
                     cols = starts_with("X"),
                     names_to = "sample",
                     values_to = "length")

library(ggplot2)
library(ggsci)
library(ggpubr)

ldata$Shoot_length <- factor(ldata$Shoot_length,
                             levels=c(unique(ldata$Shoot_length)),ordered = TRUE) 

data$Shoot_length 


ggplot(data = ldata ,aes(x = Shoot_length, y = length))+ 
  stat_boxplot(geom = "errorbar",
               width=0.1)+
  geom_boxplot(widths = 0.1)+
  geom_jitter()+
  theme_classic()+
  scale_color_aaas() +
  theme(plot.subtitle = element_text(size = 15,hjust = 0.5), 
        legend.title = element_text(size = 12))+
  labs(x = NULL, y = NULL,color = "Group")+ 
  theme(axis.text.x = element_text(size = 15),axis.text.y = element_text(size = 15))+ 
 theme(legend.text = element_text(size = 12),legend.title = element_text(size = 14))

这要是解决不了,妥妥的学术造假啊!

经过检索,我们找到了答案。其实问题非常简单,geom_boxplot() 函数里有一批专门针对离群值 (outlier) 进行标注的参数。

  • outlier.colour:离群点的颜色
  • outlier.fill:离群点的填充色
  • outlier.shape:离群点的形状
  • outlier.size:离群点的大小
  • outlier.alpha:离群点的透明度

如果我们使用geom_point(),实际上outlier只是对离群点进行了标注。默认情况下,标注会和原点属性相同(重合),因此只能看到一个点。

修改了默认参数

而一旦我们使用了geom_jitter(),那么因其“抖动”的属性,标注和原点就会分离开来,这也就造成了两个点的假象。