如何使用箭头指示的偏移 geom_text() 标签绘制 geom_tile() ?

我可以像这样通过 geom_tile() 绘制带有标签的 geom_text()

library(ggplot2)

df <- structure(list(x = c(1L,2L,3L,4L,5L,6L,7L,8L,1L,8L),y = c("A","A","B","B"),z = c("stuff","not_stuff","stuff","not_stuff")),class = "data.frame",row.names = c(NA,-16L))

plt <- ggplot2::ggplot(data = df,mapping = ggplot2::aes(x = x,y = y,fill = z)) + 
  ggplot2::geom_tile(height = ifelse(z == "stuff",0.4,0.1)) + 
  ggplot2::geom_text(ggplot2::aes(label = ifelse(z == "stuff",z,"")))

plt

如何使用箭头指示的偏移 geom_text() 标签绘制 geom_tile() ?

但我想用这样的箭头(弯曲或其他方式)将标签从瓷砖本身偏移:

如何使用箭头指示的偏移 geom_text() 标签绘制 geom_tile() ?

(抱歉画得不好。)我希望每个图块的标签都带有箭头,就像我在上图中描绘的一个例子一样。

我不知道该怎么做,而且我真的无法在其他地方找到答案。

任何帮助和/或指针将不胜感激

zk2511380123zk 回答:如何使用箭头指示的偏移 geom_text() 标签绘制 geom_tile() ?

geom_label_repel 包中的

ggrepel 非常适用于此:

library(ggplot2)
library(ggrepel)

ggplot(df,aes(x = x,y = y,fill = z)) + 
  geom_tile(height = ifelse(z == "stuff",0.4,0.1)) + 
  geom_label_repel(
    aes(label = ifelse(z == "stuff",z,"")),fill = NA,nudge_y = 0.5
  )

enter image description here

本文链接:https://www.f2er.com/2453.html

大家都在问