分组变量上的ggplot和ggsignif错误

时间:2018-12-13 14:01:08

标签: r ggplot2

尝试在geom_sig中运行ggplot时遇到此错误。

Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
Error in FUN(X[[i]], ...) : object 'Gender' not found 

我的目标是在多面图中将重要指标放置在避开点周围。我尝试遵循尽可能接近软件包自述文件的方法,但是我无法克服此错误。

此代码复制了它。

library(tidyverse)
library(ggsignif)

set.seed(123)
df <- tibble(
  Gender = c(rep('Female',15), rep('Male',15)), 
  key = paste(sample(LETTERS, 30, T), sample(1:30, 30, F), sep = '_'),
  Value_mean = rnorm(30, 3, 1), 
  n = rep(100,30),
  sd = rnorm(30, 1, .5),
  se = rnorm(30, .05, .05),
  lower.ci = Value_mean - se,
  higher.ci = Value_mean + se, 
  trun_cat = rep(LETTERS[1:5], 6)
)




significant_df <- tibble(
  trun_cat = c('A','C','E'),
  start = c('H_29', 'R_24','L_23'),
  end = start,
  label = c('*', '**', '*'),
  y = rep(4.5,3))



df %>% 
ggplot(aes(
  fct_reorder(key, Value_mean, .desc = T),
  Value_mean,
  group  = Gender,
  color = Gender,
  fill = Gender
)) +
  geom_errorbar(
    aes(ymin = Value_mean - se,
        ymax = Value_mean + se,),
    width = .1,
    position = position_dodge(0.5),
    alpha = .9,
    show.legend = F
  ) +
  geom_point(
    position = position_dodge(0.5),
    size = 4,
    show.legend = T,
    alpha = 1
  ) + 
geom_signif(
  data = significant_df,
  aes(
    xmin = start,
    xmax = end,
    annotations = label,
    y_position = y
  ),
  textsize = 3,
  vjust = -0.2,
  manual = TRUE
)+
scale_color_grey() +
  scale_fill_grey() +
  facet_grid(~ trun_cat, scales = 'free_x')

如何解决该错误?

1 个答案:

答案 0 :(得分:1)

问题是geom_signif继承了之前定义的美学,然后在Gender中寻找significant_df,但找不到。

我不确定是否能为您带来理想的结果,但是要使绘图有效,您可以添加inherit.aes = FALSE

geom_signif(
    inherit.aes = FALSE,
    ...

enter image description here