排斥ggplot中的geom标签和文本。并根据尺寸排序几何点

时间:2018-11-22 11:32:40

标签: r ggplot2 ggrepel

我有2个这样的数据帧:

df1 <- data.frame(
  party = c("Blue Party", "Red Party"),
  dim1 = c(0.03, -0.04),
  dim2 = c(-0.05, 0.02),
  sz = c(34, 42)
)
df2 <- data.frame(
  var = c("Economic", "Gov trust", "Inst trust", "Nationalism", "Religiosity"),
  dim1 = c(0.1, -0.5, 0, 0.6, 0.4),
  dim2 = c(0.1, 0.6, 0, 0, 0.3)
)

我想将df1上的各方绘制为由大小定义的点,并在同一图上包括基于df2的箭头。我已经使用ggplot来做到这一点:

ggplot(df1, aes(x = dim1, y = dim2, color = party)) +
  geom_point(size = df1$sz) +
  scale_size_area() +
  scale_x_continuous(limits = c(-1.5, 1.5)) +
  scale_y_continuous(limits = c(-1.5, 1.5)) +
  geom_label_repel(aes(label = party),
                   box.padding   = 1,
                   point.padding = 1.5,
                   force = 1) +
  geom_segment(aes(xend=0, yend=0, x=dim1, y=dim2), data=df2,
    arrow=arrow(length=unit(0.20,"cm"), ends="first", type = "closed"), color="black") +
  geom_text_repel(aes(x=dim1, y=dim2, label=var),
    data = df2, color = "black", size = 3, force = 1)

结果:

Graph 1

函数geom_label_repelgeom_text_repel可以防止聚会标签和文本重叠,但是如何互相排斥标签和文本呢?

我的第二个问题是我想对这些点进行排序,这些点的前面最小,后面最大。该怎么办?

感谢帮助!

0 个答案:

没有答案