我已经在ggploy中为组创建了一个框图.因此,我不得不将数据帧从宽格式reshape 为长格式.现在,我想要更改我的图表中各个绘图的标签或名称.我使用了函数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还很陌生,需要为我的硕士论文提供图表,并感谢任何人的帮助.首先要感谢大家!

推荐答案

问题是,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"))

R相关问答推荐

如何使用TukeyHSD绘制事后概率热图

是否有R函数来判断一个组中的所有值是否与另一个组中的所有值相同?

根据固定值范围在tible中添加新行

过滤矩阵以获得R中的唯一组合

在特定列上滞后n行,同时扩展框架的长度

在ggplot Likert条中添加水平线

无法运行通过R中的Auto.arima获得的ARIMA模型

查找具有平局的多个列的最大值并返回列名或平局 destruct 者NA值

带有gplot 2的十字舱口

基于多列将值链接到NA

将非重复序列高效转换为长格式

有效识别长载体中的高/低命中

如何提取所有完美匹配的10个核苷酸在一个成对的匹配与生物字符串在R?>

`lazy_dt`不支持`dplyr/across`?

SHINY:使用JS函数应用的CSS样式显示HTML表格

为左表中的所有行使用值Fill滚动左连接

如果COLSUM为>;0,则COLNAME为向量

当每个变量值只能 Select 一次时,如何从数据框中 Select 两个变量的组合?

随机 Select 的非NA列的行均数

按镜像列值自定义行顺序