`ggplot2` 中的 `annotate` 与 `facet_grid` 结合时会报告错误

立坎

我想在 ggplot 图中添加两个注释。当图形不包含facet_grid,例如 时p1,添加这样的annotate层可以正常工作,即q1但是,当我facet_grid向原始图形添加一个图层,即p2,然后添加相同的“注释”图层,即q2导致错误报告:

错误:Aesthetics 必须为长度 1 或与数据相同 (4):标签

有什么建议吗?谢谢。

PS,我使用的包ggplot2的版本是2.2.1。

p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() p2 <- p1 + facet_grid(vs~.) q1 <- p1 + annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2")) q2 <- p2 + annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2"))

立坎

以下是我从包作者 Hadley Wickham 那里得到的答案:

https://github.com/tidyverse/ggplot2/issues/2221

不幸的是,很难annotate()自动执行此操作。而是通过自己创建数据集来“手工”完成。

library(ggplot2)
df <- data.frame(wt = 2:3, mpg = 20:21, label = c("my label", "label 2"))
ggplot(mtcars, aes(wt, mpg)) + 
geom_point() +
geom_text(aes(label = label), data = df) + 
facet_grid(vs ~ .)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2中的facet_grid错误

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

ggplot2中的facet_wrap()和facet_grid()有什么区别?

在ggplot2中的特定构面之间添加空间(facet_grid)

如何在ggplot2中使用facet_grid制作甜甜圈图?

如何在ggplot2中按组为facet_grid着色?

在ggplot2中旋转切换的构面标签facet_grid

使用ggplot2中的facet_grid更改中断次数

ggplot2 R中的facet_grid函数的非线性拟合

变更单保证金显示在facet_grid ggplot2中?

ggplot2 facet_grid:如何修复geom_col中列之间的不同间距

ggplot2:facet_grid中x轴标签的条件格式

ggplot2:在 facet_grid 中按组添加水平线

ggplot中的facet_grid中的行式着色

控制facet_grid、ggplot中的间距和标签

如何在ggplot的facet_grid函数中应用下标

将水平条与ggplot中的facet_grid组合

将ggplot的facet_grid与bmrs conditional_effects结合

使用R中的facet_grid的ggplot2 boxplot中的表达式分别更改x标签

在 facet_grid 中将 label_both 设置为 ggplot2 中主题的默认值

在ggplot2中使用facet_grid()时如何定义常见的y轴限制

ggplot2 stat_function,具有facet_grid中不同数据子集的计算参数

在facet_grid上下文中设置ggplot2中scale_x_date的限制

重新排列facet_grid并将其添加到ggplot2的geom_bar中

在ggplot2中使用`facet_grid`时,突出显示/在某些绘图周围画一个框

r-如何在ggplot2中使用facet_grid获得长标签以适合?

使用ggplot和facet_grid指定错误栏

每条刻面线具有不同 y 轴的 ggplot2 刻面:如何从 facet_grid 和 facet_wrap 中获得“最佳”?

使用 facet_grid 在 ggplot 中對 facet 進行排序