如何注释交互图中各个方面的文本(`ggplot2`;`interactions`)?
How to annotate text on individual facets in interaction plot (`ggplot2`; `interactions`)?
我想在每个方面都有不同的注释(例如,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_group
和 drat
设置为 NA
或 NA_real_
甚至 0
,如 所示,会引发另一个错误: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 年 6 月 10 日创建
问题是 geom_text
继承了 interact_plot
的全局美学。要防止这种情况,只需添加 inherit.aes = FALSE
。尽管如此,您必须将分面变量添加到标签 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-06-09 创建
我想在每个方面都有不同的注释(例如,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_group
和 drat
设置为 NA
或 NA_real_
甚至 0
,如 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 年 6 月 10 日创建问题是 geom_text
继承了 interact_plot
的全局美学。要防止这种情况,只需添加 inherit.aes = FALSE
。尽管如此,您必须将分面变量添加到标签 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-06-09 创建