Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

r - Label ggplot2 boxplot dots with sample IDs

I have a simulated data 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

I used geom_text_repel() to label dots as recommended by a post

My code:

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()

Plot

enter image description here

My question:

How to adjust parameters to plot clear id-arrow-dot? Here, for example, id1,id2,id5 and id6 are clustered together and it’s hard to tell which sample each arrow points to.

Thanks.

question from:https://stackoverflow.com/questions/65893567/label-ggplot2-boxplot-dots-with-sample-ids

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One possible option is to jitter the data points so that they no longer overlap so heavily. This also avoids the need to bin the y-axis and use a stacked dotplot. It's not perfect but it's useful for representing the spread of your data, which it seems like the points atop the boxplot is intended to do.

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()

plot with jittered data


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...