我想在每个方面都有不同的注释(例如,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
甚至this other post会引发另一个错误:Discrete value supplied to continuous scale
。
我无法在当前情况下理解这些错误。我怀疑,当然,这与interactions
plot对象很时髦有关。可能还有一些明显的地方我做错了,但看不到。任何帮助,将不胜感激!
基于@stefan的答案,我能够为更复杂的设计创建所需的输出(具有6个p值,每个斜率一个,每个注释一个特定的位置),如下所示。不幸的是,我不得不创建3个单独的数据帧和geom_text()
调用;我无法使其工作(尝试制作一个更大的数据框只是添加了空面板,因为方面因素只有两个级别)。我希望我可以在一个电话中做到这一点。
# Create the model
mod1 <- lm(wt ~ am * drat * vs, data = mtcars)
# Make the plot
require(interactions)
(q <- interact_plot(mod1,pred="am",modx="drat",mod2="vs"))
# Make annotations dataframes
(p.values <- paste0("p-value ",rep(1:6)))
facet = c("vs = 0", "vs = 1")
x = c(0.5, 0.5)
y = c(3, 2.5, 3.5, 2.75, 4, 3)
dat_text1 <- data.frame(
text = p.values[1:2],
mod2_group = facet,
x = x,
y = y[1:2])
dat_text2 <- data.frame(
text = p.values[3:4],
mod2_group = facet,
x = x,
y = y[3:4])
dat_text3 <- data.frame(
text = p.values[5:6],
mod2_group = facet,
x = x,
y = y[5:6])
# Add annotations to dataframe
require(ggplot2)
q + geom_text(data = dat_text1,
mapping = aes(x = x, y = y, label = text),
inherit.aes = FALSE) +
geom_text(data = dat_text2,
mapping = aes(x = x, y = y, label = text),
inherit.aes = FALSE) +
geom_text(data = dat_text3,
mapping = aes(x = x, y = y, label = text),
inherit.aes = FALSE)
问题是geom_text
继承了interact_plot
的整体美学。为防止这种情况,只需添加inherit.aes = FALSE
。但是,您必须将faceting变量添加到标签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创建