stat_poly_eq() 红色文本表示 facet_wrap 中的重要 p 值
stat_poly_eq() red text for significant p-value in facetwrap
我在 facet_wrap 中包含了一系列回归,我想知道是否可以用红色文本或 stat_poly_eq 中的“*”来表示重要 pval 的文本() [ggpmisc] 函数?
这是我的示例代码
ggplot(my.data, aes(x=age, y=logRR))+
geom_hline(yintercept=0, linetype="dashed", color = "gray", size = 0.5) +
geom_ribbon(aes(ymax=eCI_95, ymin=eCI_5), group=1, alpha=0.2, fill = "#BC1605") +
geom_point(shape=19, size=3, color = "#BC1605") +
geom_smooth(method='lm', se = T, formula=y~x, linetype='solid', size=0.5) +
stat_poly_eq(formula = y ~ x,
aes(label = paste(..rr.label.., ..p.value.label.., sep = "*`,`~")),
parse = TRUE,
label.x.npc = "right",
vstep = 0.05) + # sets vertical spacing
scale_color_manual(values=c("#000000", "#A4A4A4"), drop = FALSE) +
scale_linetype_manual(values=c("solid","twodash"), drop = FALSE) +
labs(y=expression(bold(Log(Diversity[MPA]/Diversity[REF])))) +
labs(colour = 'Difference', linetype = 'Slope') +
facet_wrap(mlpa_region~group, scales="free_y", nrow=3)+
scale_x_continuous(breaks = seq(from=-5, to=19, by=2))+
theme_classic()
是的,您可以使用 ggtext 包设置颜色。
library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
library(ggtext)
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x,
y = y,
group = c("A", "B"))
formula <- y ~ poly(x, 1, raw = TRUE)
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, geom = "richtext",
label.x = 30, label.y = 10e5,
mapping = aes(
label = ifelse(after_stat(r.squared) > 0.81,
paste0(
"<span style='color:red'>",
after_stat(rr.label),
"</span>"
),
paste0(
"<span style='color:blue'>",
after_stat(rr.label),
"</span>"
)
),
label.color = NA))
由 reprex package (v2.0.1)
创建于 2022-06-02
只要您不使用色标对数据进行分组,您也可以在没有任何附加包的情况下设置颜色,因为此答案依赖于 scale_color_identity()
。
library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x,
y = y,
group = c("A", "B"))
formula <- y ~ poly(x, 1, raw = TRUE)
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula,
label.x = "left",
mapping = aes(
color = ifelse(after_stat(r.squared) > 0.81,
"red", "black"
)
)) +
scale_color_identity()
由 reprex package (v2.0.1)
于 2022-05-29 创建
我在 facet_wrap 中包含了一系列回归,我想知道是否可以用红色文本或 stat_poly_eq 中的“*”来表示重要 pval 的文本() [ggpmisc] 函数?
这是我的示例代码
ggplot(my.data, aes(x=age, y=logRR))+
geom_hline(yintercept=0, linetype="dashed", color = "gray", size = 0.5) +
geom_ribbon(aes(ymax=eCI_95, ymin=eCI_5), group=1, alpha=0.2, fill = "#BC1605") +
geom_point(shape=19, size=3, color = "#BC1605") +
geom_smooth(method='lm', se = T, formula=y~x, linetype='solid', size=0.5) +
stat_poly_eq(formula = y ~ x,
aes(label = paste(..rr.label.., ..p.value.label.., sep = "*`,`~")),
parse = TRUE,
label.x.npc = "right",
vstep = 0.05) + # sets vertical spacing
scale_color_manual(values=c("#000000", "#A4A4A4"), drop = FALSE) +
scale_linetype_manual(values=c("solid","twodash"), drop = FALSE) +
labs(y=expression(bold(Log(Diversity[MPA]/Diversity[REF])))) +
labs(colour = 'Difference', linetype = 'Slope') +
facet_wrap(mlpa_region~group, scales="free_y", nrow=3)+
scale_x_continuous(breaks = seq(from=-5, to=19, by=2))+
theme_classic()
是的,您可以使用 ggtext 包设置颜色。
library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
library(ggtext)
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x,
y = y,
group = c("A", "B"))
formula <- y ~ poly(x, 1, raw = TRUE)
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, geom = "richtext",
label.x = 30, label.y = 10e5,
mapping = aes(
label = ifelse(after_stat(r.squared) > 0.81,
paste0(
"<span style='color:red'>",
after_stat(rr.label),
"</span>"
),
paste0(
"<span style='color:blue'>",
after_stat(rr.label),
"</span>"
)
),
label.color = NA))
由 reprex package (v2.0.1)
创建于 2022-06-02只要您不使用色标对数据进行分组,您也可以在没有任何附加包的情况下设置颜色,因为此答案依赖于 scale_color_identity()
。
library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#>
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#>
#> annotate
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x,
y = y,
group = c("A", "B"))
formula <- y ~ poly(x, 1, raw = TRUE)
ggplot(my.data, aes(x, y)) +
facet_wrap(~ group) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula,
label.x = "left",
mapping = aes(
color = ifelse(after_stat(r.squared) > 0.81,
"red", "black"
)
)) +
scale_color_identity()
由 reprex package (v2.0.1)
于 2022-05-29 创建