r - 如何在交互图中的各个方面注释文本(`ggplot2`;`interactions`)?
问题描述
我想在每个方面都有不同的注释(例如 p 值)(我的实际绘图的每个斜率一个 - 总共 6 个)。我想我已经阅读了所有关于注释方面的帖子,最有用的当然是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
and或甚至drat
,如this other post所示,会引发另一个错误:.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 包(v0.3.0)于 2020 年 6 月 10 日创建
解决方案
问题是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 包(v0.3.0)于 2020 年 6 月 9 日创建
推荐阅读
- powershell - 如何将 LastWriteTime 添加到此脚本?
- python - Python 错误 AuthenticationError:(状态 400)(Quandl 错误 QEAx01)我们无法识别您的 API 密钥。请检查您的 API 密钥并重试
- batch-file - CMD:将命令的结果绑定到变量并在下一次调用中使用它
- html - 单击菜单项后关闭 Bootstrap 4 可悬停下拉菜单
- r - 如何强制 r-mapview 在弹出窗口或某些行上显示所有数据,而不考虑 zcol=
- python - RADIUS 计算 Message-Authenticator 字段(python)
- ios - 如何将 IBOutlet 值从视图传递到 VIPER iOs 中的交互器?
- ios - 在 Swift 4 上调整 UIImage 视图的大小
- ember.js - 升级 Ember 时出现未知的全局错误
- elasticsearch - 在多/所有弹性搜索嵌套字段中动态搜索