避免在ggplot2中重叠geom_point和geom_text [英] Avoid overlapping geom_point and geom_text in ggplot2

查看:1567
本文介绍了避免在ggplot2中重叠geom_point和geom_text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能避免ggplot2中的这两层重叠?我尝试显示文本,以便它们不会超出要点。

How can I avoid that these 2 layers in ggplot2 overlap? I try to display the text so that they are not laying above the points.

check_overlap 做得非常好避免文本与自身重叠,但不与其他图层重叠。

check_overlap does a great job with avoiding that the text overlaps itself, but not with other layers.

我也试过库 geom_text_repel ,但是这个库不支持 check_overlap 并显示每个数据点的文本。

I also tried the library geom_text_repel, but this library does not support check_overlap and shows the text for every data point.

但我不需要文本对于每一点,像 check_overlap 都没有。

But I need to not have the text for every point, like check_overlap does.

ggplot(dat, aes(x = CPI, y = HDI)) +
  geom_point(aes(color = Region), shape=21, size=4, position = "identity") +
  geom_text(data = dat, aes(label = Country), size=4, check_overlap = TRUE)


推荐答案

geom_text_repel 不会为空字符串>创建文本标签。但是,文本标签会排斥未标记的数据点。

geom_text_repel will not create text labels for the empty string "". However, the text labels will repel away from the unlabeled data points.

试试这个:

Try this:

# Hide text labels for the first 3 data points
idx <- c(1,2,3)

dat$CountryLabel      <- dat$Country
dat$CountryLabel[idx] <- ""

library(ggrepel)
ggplot(...) + geom_text_repel(data = dat, aes(label = CountryLabel))

这篇关于避免在ggplot2中重叠geom_point和geom_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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