【问题标题】:How to annotate text on individual facets in interaction plot (`ggplot2`; `interactions`)?如何在交互图中的各个方面注释文本(`ggplot2`;`interactions`)?
【发布时间】:2020-06-09 16:20:54
【问题描述】:

我想在每个方面都有不同的注释(例如 p 值)(我的实际绘图的每个斜率一个 - 总共 6 个)。我想我已经阅读了所有关于注释方面的帖子,最有用的当然是主要的Annotating text on individual facet in ggplot2。但在我的情况下,它会抛出错误。

我正在使用interactions 包,它提供了一个可编辑的ggplot 对象,但会带来其他问题。这是一个使用mtcars 的最小可重复示例。

# Create the model
mod1 <- lm(wt ~ am * drat * vs, data = mtcars)

# Make the plot
require(interactions)
(p <- interact_plot(mod1,pred="am",modx="drat",mod2="vs"))

# Make annotations dataframe
(dat_text <- data.frame(
  text = c("p-value 1", "p-value 2"),
  vs   = c(0, 1)))

# Add annotations to dataframe
require(ggplot2)
p + geom_text(
  data    = dat_text,
  mapping = aes(x = -Inf, y = -Inf, label = text),
  hjust   = -0.1,
  vjust   = -1
)

这给出:Error in FUN(X[[i]], ...) : object 'modx_group' not found'drat' not found 也有同样的错误。我不太确定如何解决这个错误(例如,将它们设置为什么值),所以我尝试将这些列添加到数据框中,如下所示:

# Make annotations dataframe
(dat_text <- data.frame(
  text = c("p-value 1", "p-value 2"),
  vs   = c(0, 1),
  modx_group = c("-1 SD", "+ 1 SD"), # Here ***
  drat = c(-1,1))) # Here ***

# Add annotations to dataframe
p + geom_text(
  data    = dat_text,
  mapping = aes(x = -Inf, y = -Inf, label = text),
  hjust   = -0.1,
  vjust   = -1
)

但这给出了:Insufficient values in manual scale. 4 needed but only 3 provided。将modx_groupdrat 设置为NANA_real_ 甚至0,如this other post 所示,会引发另一个错误:Discrete value supplied to continuous scale

在当前上下文中我无法理解这些错误。当然,我怀疑这与interactions 情节对象的时髦有关。也可能有一些明显的我做错了但看不到。任何帮助将不胜感激!

编辑

根据@stefan 的回答,我能够为我更复杂的设计创建所需的输出(有 6 个 p 值,每个斜率一个,每个注释的特定位置),如下所示。

<!-- language-all: lang-r -->


# Create the model
mod1 <- lm(wt ~ am * drat * vs, data = mtcars)

# Make the plot
require(interactions)
#> Loading required package: interactions
(p <- interact_plot(mod1,pred="am",modx="drat",mod2="vs"))

# Make annotations dataframes
dat_text <- data.frame(
  text = c("p-value 3", "p-value 6", "p-value 2", "p-value 5", "p-value 1", "p-value 4"),
  mod2_group = c("vs = 0", "vs = 1", "vs = 0", "vs = 1", "vs = 0", "vs = 1"),
  x = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5),
  y = c(3, 2.5, 3.5, 2.75, 4, 3))

# Add annotations to dataframe
require(ggplot2)
#> Loading required package: ggplot2
p + geom_text(data = dat_text,
              mapping = aes(x = x, y = y, label = text),
              inherit.aes = FALSE)

reprex package (v0.3.0) 于 2020-06-10 创建

【问题讨论】:

    标签: r ggplot2 facet interaction annotate


    【解决方案1】:

    问题在于geom_text 继承了interact_plot 的全局美学。为了防止这种情况,只需添加inherit.aes = FALSE。尽管如此,您必须将 facetting 变量添加到标签 df。要防止 ggplot2 为图例中的文本添加字形,只需添加 show.legend = FALSE

    # Create the model
    mod1 <- lm(wt ~ am * drat * vs, data = mtcars)
    
    # Make the plot
    require(interactions)
    #> Loading required package: interactions
    p <- interact_plot(mod1,pred="am",modx="drat", mod2="vs")
    
    # Have a look at the dataframe
    p$data
    #> # A tibble: 600 x 6
    #>       wt  drat    vs     am modx_group mod2_group
    #>    <dbl> <dbl> <dbl>  <dbl> <fct>      <fct>     
    #>  1  4.13  3.06     0 0      - 1 SD     vs = 0    
    #>  2  4.12  3.06     0 0.0101 - 1 SD     vs = 0    
    #>  3  4.12  3.06     0 0.0202 - 1 SD     vs = 0    
    #>  4  4.11  3.06     0 0.0303 - 1 SD     vs = 0    
    #>  5  4.11  3.06     0 0.0404 - 1 SD     vs = 0    
    #>  6  4.10  3.06     0 0.0505 - 1 SD     vs = 0    
    #>  7  4.10  3.06     0 0.0606 - 1 SD     vs = 0    
    #>  8  4.09  3.06     0 0.0707 - 1 SD     vs = 0    
    #>  9  4.09  3.06     0 0.0808 - 1 SD     vs = 0    
    #> 10  4.08  3.06     0 0.0909 - 1 SD     vs = 0    
    #> # ... with 590 more rows
    
    (dat_text <- data.frame(
      text = c("p-value 1", "p-value 2"),
      mod2_group   = c("vs = 0", "vs = 1")))
    #>        text mod2_group
    #> 1 p-value 1     vs = 0
    #> 2 p-value 2     vs = 1
    
    
    # Add annotations to dataframe
    require(ggplot2)
    #> Loading required package: ggplot2
    p + geom_text(
      data    = dat_text,
      mapping = aes(x = .5, y = 2, label = text), inherit.aes = FALSE, show.legend = FALSE,
      hjust   = -0.1,
      vjust   = -1
    )
    

    reprex package (v0.3.0) 于 2020 年 6 月 9 日创建

    【讨论】:

    • 很抱歉我的问题不清楚。我不是在询问 x y 绘图标签,而是如何注释(即直接在绘图上添加文本,而不是更改标签),例如单个斜率的 p 值。为了更清楚,我将“p-value 1”和“p-value 2”更改为“test1”和“test2”。
    • 好的,首先,谢谢!它似乎正在工作。我注意到 modx_group = c("- 1 SD", "- 1 SD") 在这里真的很重要。因为它可以与您复制的代码一起使用,但在我输入时却不行。最终我注意到唯一的区别是我在减号和 1 SD 之间缺少一个空格。解决此问题后,您实际上并不需要 wt 并在其中。另一件需要注意的是 mod2_group 和 modx_group 都必须有这些特定的值,否则它不起作用。也许您可以在为未来读者的回答中对此发表评论。
    • 这个解决方案不能完全为我工作的唯一另一个问题是它在图例中为每一行 drat 级别添加了不需要的字母“a”。我看不出它为什么会这样做的明显解释。但是,这有点问题。如果我们也能解决这个新问题,我一定会接受你的回答。
    • 不是什么大问题。等一下。我会想出一个更好的解决方案。 (;
    • 现在我知道inherit.aes 有什么用处了……到目前为止还没用过。
    猜你喜欢
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多