为什么我的箱线图中的所有条形图都集中在一起?

问题描述 投票:0回答:1

我尝试为每个日期创建箱线图。但它们没有按日期分发。相反,所有日期的数据都收集在一起。

原始数据如下:

df<-structure(list(traitement = c("WS", "WW", "WW", "WS", "WW", "WS", 
"WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WS", "WW", "WS", 
"WW", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", 
"WW", "WS", "WS", "WW", "WS", "WW", "WW", "WS", "WW", "WS", "WW", 
"WS", "WW", "WS", "WW", "WS", "WW", "WS", "WS", "WW", "WS", "WW", 
"WW", "WS", "WW", "WS", "WW", "WS", "WW", "WS", "WS", "WW"), 
    Variete = c("Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", 
    "Heihe", "Heihe", "Heihe", "Heihe", "Heihe", "Heihe"), Date_obs = structure(c(1654128000, 
    1654128000, 1654128000, 1654128000, 1654128000, 1654128000, 
    1654128000, 1654128000, 1654128000, 1654128000, 1654128000, 
    1654128000, 1654128000, 1654128000, 1654128000, 1654128000, 
    1654732800, 1654732800, 1654732800, 1654732800, 1654732800, 
    1654732800, 1654732800, 1654732800, 1654732800, 1654732800, 
    1654732800, 1654732800, 1654732800, 1654732800, 1654732800, 
    1654732800, 1654905600, 1654905600, 1654905600, 1654905600, 
    1654905600, 1654905600, 1654905600, 1654905600, 1654905600, 
    1654905600, 1654905600, 1654905600, 1654905600, 1654905600, 
    1654905600, 1654905600, 1655337600, 1655337600, 1655337600, 
    1655337600, 1655337600, 1655337600, 1655337600, 1655337600, 
    1655337600, 1655337600, 1655337600, 1655337600), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), SF_Plante_Totale = c(72420.3104, 
    29114.0672, 75556.9088, 74015.0912, 78418.8832, 70472.4416, 
    112207.4432, 88377.9264, 69432.7936, 92212.8544, 77802.9408, 
    82542.1664, 88109.1872, 76698.56, 80511.9104, 79568.3808, 
    155241.024, 60505.552, 166123.9808, 155033.0944, 169152.6912, 
    161068.9376, 225060.2528, 196524.8576, 140758.5312, 183907.8464, 
    165533.5392, 179435.3984, 154201.376, 165504.1152, 173026.8512, 
    169172.3072, 181198.8768, 77153.6512, 194143.4752, 179708.0608, 
    191554.1632, 183735.2256, 250527.7056, 217831.7568, 167404.9056, 
    217874.912, 195002.656, 210768.0352, 164633.1648, 188760.8448, 
    200502.9824, 196662.1696, 198612, 159042.6048, 297564.912, 
    201389.6256, 267434.736, 227388.672, 264551.184, 223802.8672, 
    195124.2752, 201203.2736, 214950.1664, 273315.6128)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -60L))

这是我使用的代码:

library(outliers)
library(ggplot2)
library(ggrepel)
df$Date_obs <- as.Date(df$Date_obs, format = "%Y-%m-%d")
    
    ggplot(df, aes(x = Date_obs, y = SF_Plante_Totale, fill = traitement)) +
      geom_boxplot(position = position_dodge2(width = 0.8), width = 0.7) +
      labs(title = "Heihe",x = "Date", y = "Totale leaf area") +
      scale_x_date(date_labels = "%Y-%m-%d", date_breaks = "1 day") +
      theme_minimal()+
      theme(axis.text.x = element_text(angle = 45, hjust = 1))

这是我得到的图:

这是我期望的效果图,但是横坐标上的日期不连续。并且可以只显示图中的异常值吗?我尝试了

geom_text_repel
,但它给了我所有的值,这使得图形非常混乱。

r ggplot2 boxplot outliers geom-text
1个回答
0
投票

因为你的x变量是连续的,你可能需要指定分组变量作为x变量和填充变量之间的交互:

ggplot(df, aes(x = Date_obs, y = SF_Plante_Totale, fill = traitement,
               group = interaction(Date_obs, traitement))) +
  geom_boxplot(position = position_dodge2(width = 0.8), width = 0.7) +
  labs(title = "Heihe",x = "Date", y = "Totale leaf area") +
  scale_x_date(date_labels = "%Y-%m-%d", date_breaks = "1 day") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

© www.soinside.com 2019 - 2024. All rights reserved.