2

我正在绘制一个包裹的 ggscatter ,如下图所示。我想要的是根据 R 和 P 值进行不同的着色。例如,当 P 不显着时,我想要绘图灰色;当 P 显着时,需要根据 R 值以连续比例着色的图。问题是我不知道如何让这些值在 ggscatter 中创建 if 语句。任何人都可以帮助我吗?谢谢!

数据集示例:

conc   exposure   col
11.16  21294      0.139275104
11.16  18018      0.150012216
13.8   26208      0.067379679
18.1   29484      0.013190731  

阴谋:

ggscatter(data, x = "exposure", y = col, add = "reg.line", conf.int = TRUE, color = cor.hilab[1], cor.coef = TRUE) +
    facet_wrap(~conc)+
    ylab("OD")+
    xlab("Exposure")

ggscatter

4

1 回答 1

1

这现在在 GitHub 的包 'ggpmisc' 中实现(未来版本 0.4.4),但尚未在 CRAN 中可用的版本 0.4.3 中实现。这并不完全按照您的要求做,因为目前geom_poly_eq()不会返回R,因为它对于高于 1 的多项式没有意义。保持图形范式的语法允许更容易定制,但代价是比全输入更长的代码-一个来自包的函数pubr。每种方法都有其优点和缺点。我在这里展示了三个使用包'ggpmisc'(连同'ggplot2')的不同可能方法的例子。

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate

# First approach: faint lines for non-significant fits, with bands
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
                                 after_stat(p.value.label),
                                 sep = "*\", \"*")),
               label.x = "right") +
  stat_poly_line(aes(colour = stage(after_scale = ifelse(p.value < 0.05, 
                                                         alpha(colour, 1), 
                                                         alpha(colour, 0.25)))),
                 se = TRUE,
                 mf.values = T) + 
  facet_wrap(~class, ncol = 2) +
  theme_bw()

# Second approach: faint lines for non-significant fits, no-bands
# colour mapped to class
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
                                 after_stat(p.value.label),
                                 sep = "*\", \"*")),
               label.x = "right") +
  stat_poly_line(aes(colour = stage(start = class,
                                    after_scale = ifelse(p.value < 0.05, 
                                                         alpha(colour, 1), 
                                                         alpha(colour, 0.25)))),
                 se = FALSE,
                 mf.values = T) + 
  facet_wrap(~class, ncol = 2) +
  theme_bw()
#> Warning: Failed to apply `after_scale()` modifications to legend

# Third approach: no bands or lines for non-significant fits
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  stat_poly_eq(aes(label = paste(after_stat(rr.label),
                                 after_stat(p.value.label),
                                 sep = "*\", \"*")),
                   label.x = "right") +
  stat_poly_line(aes(colour = stage(after_scale = ifelse(p.value < 0.05, 
                                                        colour, 
                                                        NA)),
                     fill = stage(after_scale = ifelse(p.value < 0.05, 
                                                       fill, 
                                                       NA))),
                 se = TRUE,
                 mf.values = T) + 
  facet_wrap(~class, ncol = 2) +
  theme_bw()
#> Warning: Duplicated aesthetics after name standardisation: NA

#> Warning: Failed to apply `after_scale()` modifications to legend

reprex 包(v2.0.1)于 2021-09-07 创建

于 2021-09-07T09:02:17.400 回答