【问题标题】:Label ggplot2 boxplot dots with sample IDs用样本 ID 标记 ggplot2 箱线图点
【发布时间】:2021-01-25 22:58:18
【问题描述】:

我有一个模拟数据cp

id       val.     tag
id1     12.60      A
id2     12.48      A
id3     12.48      B
id4     13.95      B
id5     12.60      A
id6     12.48      A
id7     11.13      A
id8     9.56       A
id9     10.32      B
id10    9.569      B

我使用geom_text_repel() 来标记post 推荐的点

我的代码:

ggplot(cp, aes(x=tag, y=val,label=id)) + geom_boxplot()+ geom_dotplot(binaxis='y', stackdir='center', dotsize=1)+geom_text_repel(arrow = arrow(length = unit(0.02, "npc")),box.padding = 1) + theme_bw()

情节

我的问题:

如何调整参数以清除id-arrow-dot?例如,这里 id1,id2,id5 和 id6 聚集在一起,很难分辨每个箭头指向哪个样本。

谢谢。

【问题讨论】:

  • 他们不是有相同的值并且属于同一个标签吗?
  • @Tung 是的,对于重叠的点,这有点棘手。即使 id1 和 id5 具有相同的值,箭头也应该指向各自的点,而不是两个箭头指向单个点。

标签: r ggplot2 plot


【解决方案1】:

一种可能的选择是jitter 数据点,这样它们就不再重叠太多。这也避免了对 y 轴进行装箱并使用堆叠点图的需要。它并不完美,但它对于表示数据的传播很有用,这似乎是箱线图顶部的点打算做的。

cp <- readr::read_table("id       val     tag
id1     12.60      A
id2     12.48      A
id3     12.48      B
id4     13.95      B
id5     12.60      A
id6     12.48      A
id7     11.13      A
id8     9.56       A
id9     10.32      B
id10    9.569      B")

cp$tag <- factor(cp$tag)
# cp$val <- jitter(cp$val, amount = 0.5)

ggplot(cp, aes(x=tag, y=val,label=id)) + 
  geom_boxplot() + 
  geom_point(size=3) +
  geom_text_repel(arrow = arrow(length = unit(0.02, "npc")),box.padding = 1) + 
  theme_bw()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多