避免使用ggplot2 :: coord_polar重叠文本标签

问题描述

我有一个在极坐标系上具有点的图。每个点都有一个关联的标签,该标签应以给定角度显示在绘图周围。可以使用axis.text或geom_text来实现;我在这里使用了geom_text。不幸的是,文本标签重叠。使用position = position_jitter()显然只允许按高度而不是按宽度抖动(即不能解决问题)。 MWE:

df <- data.frame("angle" = runif(50,359),"projection" = runif(50,1),"labels" = paste0("label_",1:50))

ggplot(data = df,aes(x = angle,y = projection,label = labels)) +
  geom_point() +
  coord_polar() + 
  theme_minimal() +
  geom_text(aes(x=angle,y=1.1,label=labels),size = 3)

enter image description here

我想使标签抖动,以使它们不会重叠,而是留在打印区域之外。我也尝试过使用angle参数有条件地倾斜标签以腾出更多空间,但无法找出正确的公式来使角度起作用。


编辑:这是创建图的另一种方法,使用scale_x_continuous将标签创建为axis.text.x。但是,这确实会导致标签重叠。

ggplot(data = df,label = labels)) +
  geom_point() +
  coord_polar() +
  scale_x_continuous(limits = c(0,360),expand = c(0,0),breaks = df$angle,labels=df$labels) +
  theme_minimal() + 
  theme(panel.grid = element_blank())

解决方法

ggrepel在这种情况下会很好地工作。

library(ggplot2)
library(ggrepel)
df <- data.frame("angle" = runif(50,359),"projection" = runif(50,1),"labels" = paste0("label_",1:50))


ggplot(data = df,aes(x = angle,y = projection,label = labels)) +
    geom_point() +
    coord_polar() + 
    theme_minimal() +
    geom_text_repel(size = 3)

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...