使用geom_text在ggplot2中的各个方面注释文本 [英] Annotating text on individual facet in ggplot2 using geom_text

查看:3443
本文介绍了使用geom_text在ggplot2中的各个方面注释文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我在每个构面上都获得了文本,但该文本是叠加的:"X1"和"X2"叠加在每个构面上. 我的代码中的问题出在哪里?

With the following code, I obtain text on each facet but the text is superimposed : "X1" and "X2" are superimposed on each facet. Where is the problem in my code ?

R代码:

new_df <- data.frame(f = as.factor(rep(1:2, 15)), x = rnorm(30), y = runif(30))
g <- ggplot(data = new_df, aes(x = x, y = y)) + geom_point() 
g <- g + facet_grid(. ~ f)
g
label_graph <- data.frame(label = c("X1", "X2"))

g <- g + geom_text(data = label_graph,
                    mapping = aes(x = Inf, y = -Inf, label = label),
                    hjust = 1.1, vjust = -1.1)
g

推荐答案

是否可以直接标记构面标头而不是标记底角?您的示例表明这可能是一个更好的解决方案.如果这适合您的实际用例,则可以直接通过粘贴X来直接更改f列,然后得到:

Is it an option for you to label the facet headers directly instead of labelling the bottom corners? Your example suggests that may be a better solution. If that works for your actual use case, you may simply change your f column directly by pasting the X, and you get:

new_df <- data.frame(f = as.factor(paste0("X", rep(1:2, 15))), x = rnorm(30), y = runif(30))
g <- ggplot(data = new_df, aes(x = x, y = y)) + geom_point() 
g <- g + facet_grid(. ~ f)
g

这篇关于使用geom_text在ggplot2中的各个方面注释文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆