【问题标题】:Incorrect legend display, and 'Warning: Computation failed in `stat_signif()`:' while running 'geom_signif' code图例显示不正确,以及运行“geom_signif”代码时“警告:“stat_signif()”中的计算失败:”
【发布时间】:2021-11-05 22:13:27
【问题描述】:

大家晚上好,

我的图表有两个主要问题。第一个是图例显示不正确的形状。它不是显示平均值的形状,而是显示异常值的形状 please see attached.

此外,当我尝试运行时

geom_signif(comparisons = list(c("Blank", "Vehicle - Vehicle")), map_signif_level = TRUE, colour = "#cf1b1b") 代码,I am getting the following error

我不明白 R 要求什么值。一开始,我只是想可能是因为数据没有标准化并且我正在使用小值,它只是在 y 轴上没有足够的空间来显示map_signif_level = TRUE,但在另一个示例中,它显示了map_signif_level = TRUEcorrectly。所以,我不明白为什么它有时会正确显示my_comparisons,有时却不正确。

这些是我用来生成上图的以下代码:

library(tidyverse)
library(ggplot2)
library(ggpubr)
ggplot(PhospholipaseA2NOPo1, aes( x = Drug, y = Absorbance))+
  geom_line(aes(linetype='median'),size=1)+
  geom_point(aes(shape='mean'),size=3)+
  ggtitle("Comparison of the effect of Blank, Vehicle - Vehicle, Vehicle - Nicotinic Acid, SP - Vehicle, and SP -Nicotinic Acid on microglia - Phospholipase A2 Assay ") +
  labs(x = expression(paste("Treatment")))+
       ylab(expression(paste("Absrobance " ~ "(",   A[425], " - ", A[600] ~ nm, ")"))) +
  stat_summary(fun=mean, colour="black", geom="point", 
               shape=18, size=3) + 
  geom_point() +
  scale_x_discrete(labels = c("Blank", "Vehicle- Vehicle", "Vehicle - Nicotinic Acid", "SP - Vehicle", "SP - Nicotinic Acid"))+
  stat_compare_means(label.y = 0.50,
                     label.x = 3.25)+
          geom_boxplot(fill=c("#2171B5","#DEEBF7", "#CC79A7", "#CC79A7", "#CC79A7"), width=0.2,notch = FALSE)+
  geom_signif(comparisons = list(c("Blank", "Vehicle - Vehicle")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Blank", "Vehicle - Nicotinic Acid")), map_signif_level = TRUE,  colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Blank", "SP - Vehicle")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Blank", "SP - Nicotinic Acid")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Vehicle - Vehicle", "Vehicle - Nicotinic Acid")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Vehicle - Vehicle", "SP - Vehicle")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Vehicle - Vehicle", "SP - Nicotinic Acid")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Vehicle - Nicotinic Acid", "SP - Vehicle")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("Vehicle - Nicotinic Acid", "SP - Nicotinic Acid")), map_signif_level = TRUE, colour = "#cf1b1b")+
  geom_signif(comparisons = list(c("SP - Vehicle", "SP - Nicotinic Acid")), map_signif_level = TRUE, colour = "#cf1b1b")+
                  theme_bw() +
        theme(panel.grid.major = element_line(colour = "#d3d3d3"),
              panel.grid.minor = element_blank(),
              panel.border = element_blank(), panel.background = element_blank(),
              plot.title = element_text(size = 14, face = "bold"))+
                   theme(axis.text.x = element_text(face="bold", color="#3142b0", 
                           size=8.9, angle=0),
                         axis.text.y = element_text(face="bold", color="#3142b0", 
                           size=8.9, angle=0))+
  scale_linetype_manual('Legend',values='solid')+
  scale_shape_manual('',values = 18)+
  theme(legend.spacing.y = unit(0.01, "cm"))

为了生成上图,我使用了the following data

非常感谢您的热心帮助。

【问题讨论】:

    标签: r ggpubr


    【解决方案1】:

    这将是一个不完整的答案,因为我不确定如何修复图例,但这是一个好的开始:

    PhospholipaseA2NOPo1<-structure(list(Drug = structure(c(1L, 1L, 5L, 5L, 5L, 5L, 5L, 
                                           5L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
                                           2L, 2L, 2L), .Label = c("Blank", "SP.N", "SP.V", "V.N", "V.V"
                                           ), class = "factor"), Absorbance = c(0.178155098, 0.281888047, 
                                                                                0.315468607, 0.395427286, 0.443773761, 0.274327232, 0.126408569, 
                                                                                0.160375886, 0.332019768, 0.328779358, 0.31918001, 0.202597307, 
                                                                                0.241052828, 0.332138474, 0.356656925, 0.329787758, 0.291912398, 
                                                                                0.264823437, 0.167194165, 0.176451481, 0.406376062, 0.28900749, 
                                                                                0.298281464, 0.181890255, 0.245723697, 0.350279692)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                          -26L))
    
    library(tidyverse)
    library(ggplot2)
    library(ggpubr)
    ggplot(PhospholipaseA2NOPo1, aes( x = Drug, y = Absorbance))+
      ggtitle("Comparison of the effect of Blank, Vehicle - Vehicle, Vehicle - Nicotinic Acid, SP - Vehicle, and SP -Nicotinic Acid on microglia - Phospholipase A2 Assay ") +
      labs(x = expression(paste("Treatment")))+
      ylab(expression(paste("Absrobance " ~ "(",   A[425], " - ", A[600] ~ nm, ")"))) +
    
      geom_point() +
      scale_x_discrete(labels = c("Blank", "Vehicle- Vehicle", "Vehicle - Nicotinic Acid", "SP - Vehicle", "SP - Nicotinic Acid"))+
      stat_compare_means(label.y = 0.50,
                         label.x = 3.25)+
      geom_boxplot(fill=c("#2171B5","#DEEBF7", "#CC79A7", "#CC79A7", "#CC79A7"), width=0.2,notch = FALSE)+
      stat_summary(fun=mean, colour="black", geom="point", 
                   shape=18, size=3) + 
      geom_signif(comparisons = split(t(combn(levels(PhospholipaseA2NOPo1$Drug), 2)), seq(nrow(t(combn(levels(PhospholipaseA2NOPo1$Drug), 2))))), 
                  map_signif_level = TRUE, colour = "#cf1b1b", step_increase = .1)+
      theme_bw() +
      theme(panel.grid.major = element_line(colour = "#d3d3d3"),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(), panel.background = element_blank(),
            plot.title = element_text(size = 14, face = "bold"))+
      theme(axis.text.x = element_text(face="bold", color="#3142b0", 
                                       size=8.9, angle=0),
            axis.text.y = element_text(face="bold", color="#3142b0", 
                                       size=8.9, angle=0))+
      scale_linetype_manual('Legend',values='solid')+
      scale_shape_manual('Mean',values = )+
      theme(legend.spacing.y = unit(0.01, "cm"))
    

    平均而言,您将统计摘要放在箱线图之前,因此它不会显示,因为箱子放在它上面。话虽如此,我不知道如何把它放在传说中,所以这就是这个答案不完整的地方。您与geom_point(aes(shape='mean'),size=3) 一起使用的技术将塑造图表上的所有点,而不仅仅是平均值,这就是我删除它的原因。至于意义,我认为问题在于您告诉它根据新的 x 轴标度而不是药物本身的实际名称来确定意义。因此,如果您将它们更改为“SP.N”而不是“SP - 烟酸”,我认为它会起作用。为简单起见,我只添加了一行来比较所有级别。

    祝你好运,希望有更擅长图表的人能够在图例方面提供更多帮助!

    【讨论】:

    • 哇,为了澄清,因为我从来没有越过structure(list(...) 代码,你能解释一下1L, 1L, 5L, 5L, 5L, 5L, 5L, 代表什么吗?感谢您的宝贵时间
    • 我将您的数据放入数据框中,然后使用“dput()”帮助重新创建它。从我在许多帖子中看到的情况来看,人们会使用 dput,因此其他人很容易复制和使用数据。检查?dput 以了解更多信息,因为它可能比我给出的描述要好得多,哈哈。我不完全确定 1L 信息是什么,但它与数据框本身的结构有关,所以可能是长度或其他什么?
    • 谢谢。我将根据您的回答阅读有关?dput 的信息,它极大地改变了一切并使事情变得更容易。我认为出于重要性,您必须为要显示的每个重要值添加 geom_signif(comparisons = list(c 的每一行。但似乎您需要将数据放入 class = "data.frame" 并完成工作。
    【解决方案2】:

    解决方案

    我已经为我的传奇找到了解决方案。首先,在使用 scale_color_manual 时,我们必须添加另一个aes 映射,以便生成任何类型的图例。因此,我添加了geom_point()geom_point(aes(colour = "Blank (50 µM of dH20)"))。然后,有了aes 映射,我可以使用以下代码生成我的图例:

    scale_color_manual(name = "Final concentrations and volumes",
                         labels = c( "Blank (50 µL of dH20)", "Vehicle - Vehicle (10µL of dH20 + 10µL of dH20)", "Vehicle -Nicotinic Acid (10µL of dH20 + 10µL of 1mM)", "Sp-Vehicle (10µL of 5µM + 10µL of dH20)", "Sp-Nicotinic Acid( 10µL of 5µM + 10µL of 1mM)"),
                         values = c("Blank (50 µL of dH20)" =  "#B2182B", "Vehicle - Vehicle (10µL of dH20 + 10µL of dH20)" = "#D6604D", "Vehicle -Nicotinic Acid (10µL of dH20 + 10µL of 1mM)" = "#F4A582", "Sp-Vehicle (10µL of 5µM + 10µL of dH20)"= "#FDDBC7", "Sp-Nicotinic Acid(10µL of 5µM + 10µL of 1mM)"= "#D1E5F0"))+
    

    有了这个,我可以使用以下代码更改我的图例位置:

    theme(legend.position = c(0, .97),
            legend.justification = c("left", "top"),
    

    我还希望我的图例的背景是透明的,所以我使用了以下代码:

    legend.background = element_rect(colour = NA, fill = NA))
    

    结果我得到了以下outcome

    现在,我正在研究如何为 H20 添加下标。我知道我可以使用expressionbquote,但到目前为止我得到了一些like this。我想我搞砸了“*”之类的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-29
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多