r - 如何在着色点时将文本添加到分面图?
问题描述
我正在尝试将文本添加到我可以使用的多面图
datasets_text <- data.frame(cyl = unique(mtcars$cyl))
datasets_text$label <- c('text1','text2','text3')
mtcars <- head(mtcars)
ggplot(mtcars, aes(hp,drat))+
geom_point()+
facet_wrap(~cyl)+
geom_text(size = 2,
data = datasets_text,
mapping = aes(x = Inf, y = Inf, label = label),
hjust = 1.05,
vjust = 1.5)
我也想给我能做的点上色
mtcars <- head(mtcars)
ggplot(mtcars, aes(hp,drat, colour=gear))+
geom_point()+
facet_wrap(~cyl)+
但是,当我将两者结合起来时
ggplot(mtcars, aes(hp,drat, colour=gear))+
geom_point()+
facet_wrap(~cyl)+
geom_text(size = 2,
data = datasets_text,
mapping = aes(x = Inf, y = Inf, label = label),
hjust = 1.05,
vjust = 1.5)
我明白了Error in FUN(X[[i]], ...) : object 'gear' not found
。如何在为点着色的同时向构面添加文本?
解决方案
您可以inherit.aes = FALSE
在调用中指定geom_text
:
ggplot(mtcars, aes(hp,drat, colour=gear))+
geom_point()+
facet_wrap(~cyl)+
geom_text(size = 2,
data = datasets_text,
mapping = aes(x = Inf, y = Inf, label = label),
hjust = 1.05,
vjust = 1.5, inherit.aes = FALSE)
从有关的geom_text
帮助文件inherit.aes
中:
如果为 FALSE,则覆盖默认美学,而不是与它们结合。这对于定义数据和美学的辅助函数最有用,并且不应该从默认绘图规范继承行为
推荐阅读
- python - 为 Keras 顺序模型格式化 networkx 数据
- django - Django:用另一个表中的字段注释(一对多)
- node.js - 带有 axios 的 Nodemon 服务器 - 无法发送
- python - 在类中使用 Beautiful Soup 查找字符串
- apache-spark - Spark Streaming - Kafka 集成
- javascript - 材料表列标题旁边的添加按钮
- reactjs - 创建新的 React 应用程序时遇到麻烦
- sql - Oracle 11g - 将多个值传递给 XML 属性
- python-3.x - 如何在 Python 中导入模块
- powershell - 有没有办法使用 SharePoint/office 365 向用户发送带有附件的电子邮件?