分组变量上的ggplot和ggsignif错误 [英] ggplot and ggsignif error on grouping variable
问题描述
尝试在 ggplot
中运行 geom_sig
时出现此错误.
I'm getting this error when trying to run a geom_sig
within ggplot
.
Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
Error in FUN(X[[i]], ...) : object 'Gender' not found
我的目标是在多面图中将重要的指示器放置在避开点周围.我尝试遵循尽可能接近软件包自述文件的方法,但是我无法克服此错误.
My goal is to place significant indicators around dodged points in a faceted plot. I've tried to follow as close to package readme as possible, but I can't get past this error.
此代码复制了它.
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')
如何解决该错误?
推荐答案
问题是 geom_signif
继承了之前定义的美学,然后在中寻找
性别
不重要的.
The issue is that geom_signif
inherits the aesthetics define before and then looks for Gender
in significant_df
, which it doesn't find.
我不确定是否能给您满意的结果,但是要使绘图有效,您可以添加 inherit.aes = FALSE
:
I'm not sure if that gives you a desired result, but to make the plot work you can add inherit.aes = FALSE
:
geom_signif(
inherit.aes = FALSE,
...
这篇关于分组变量上的ggplot和ggsignif错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!