如何在R中重命名我的单个箱线图的标签?

njthzxwz  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(158)

我在ggplot中创建了一个组的箱线图。因此,我不得不将 Dataframe 从宽格式重塑为长格式。现在我想更改图形中各个图的标签或名称。我使用了函数xlim(labels= c("A", "B"),它改变了标签,但给了我错误消息,不再计算/显示绘图。

data3 %>% 
      select(Treatment_A, Treatment_B) %>%
      pivot_longer(cols = everything(), names_to = "Variable",values_to = "Value") %>%
      ggplot(aes(x= Variable, y = Value, fill = Variable)) +
      stat_boxplot(geom= "errorbar", width= 0.5) +
      geom_boxplot(fill=c("skyblue2", "gold2"))+
      labs(x="Treatment", y="Frequency (total)", title="Treatment comparison")+
      theme_minimal()+
      theme(axis.title.x = element_text(size =10))+
      theme(axis.title.y = element_text(size =10))+
      theme(plot.title=element_text(size = 16))+
      theme(plot.title = element_text(hjust = 0.5))+
      stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
               width = .75,lwd = 0.7, linetype = "dashed", col = "red") +
      xlim(labels= c("A", "B"))+ # <- This gives me the error
      ylim(0,30)

警告消息:

1: Removed 2730 rows containing missing values (stat_boxplot). 
2: Removed 2730 rows containing missing values (stat_boxplot). 
3: Removed 2730 rows containing non-finite values (stat_summary). 
4: In max(f) : no non-missing arguments to max; returning -Inf
5: Computation failed in `stat_summary()`:
argument must be coercible to non-negative integer

我对R还很陌生,需要我的硕士论文的图形,并感谢任何帮助。非常感谢提前!

h43kikqp

h43kikqp1#

问题是xlim是设置限制的简写,使用xlim(labels= c("A", "B"))您将x刻度的限制设置为等于c("A", "B"),即图中仅包括x值为"A""B"的观测。所有其他观察都被丢弃,这就是警告所告诉的。
要指定标签,请使用scale_x_discretelabels参数。
使用一些假随机示例数据:

library(tidyverse)

set.seed(123)

data3 <- data.frame(
  Treatment_A = runif(100, 0, 30),
  Treatment_B = runif(100, 0, 30)
)

p <- data3 %>%
  select(Treatment_A, Treatment_B) %>%
  pivot_longer(cols = everything(), names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = Variable, y = Value, fill = Variable)) +
  stat_boxplot(geom = "errorbar", width = 0.5) +
  geom_boxplot(fill = c("skyblue2", "gold2")) +
  labs(x = "Treatment", y = "Frequency (total)", title = "Treatment comparison") +
  theme_minimal() +
  theme(axis.title.x = element_text(size = 10)) +
  theme(axis.title.y = element_text(size = 10)) +
  theme(plot.title = element_text(size = 16)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  stat_summary(
    fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
    width = .75, lwd = 0.7, linetype = "dashed", col = "red"
  ) +
  ylim(0, 30)

首先,通过添加xlim(labels = c("A", "B"))来重现您的问题:

p +
  xlim(labels = c("A", "B"))
#> Warning: The dot-dot notation (`..y..`) was deprecated in ggplot2 3.4.0.
#> ℹ Please use `after_stat(y)` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: Removed 200 rows containing missing values (`stat_boxplot()`).
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Warning in min(diff(sort(x))): no non-missing arguments to min; returning Inf
#> Warning: Removed 200 rows containing missing values (`stat_boxplot()`).
#> Warning: Removed 200 rows containing non-finite values (`stat_summary()`).
#> Warning in max(f): no non-missing arguments to max; returning -Inf
#> Warning: Computation failed in `stat_summary()`
#> Caused by error in `seq_len()`:
#> ! argument must be coercible to non-negative integer

使用scale_x_discrete可以得到所需的结果:

p +
  scale_x_discrete(labels = c("A", "B"))

相关问题