如何在 R 中生成与此类似的抖动图? [英] How to generate a jitterplot similar to this one in R?

查看:70
本文介绍了如何在 R 中生成与此类似的抖动图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中重现一个类似于 Zack 等人,Nature Genetics,2013 年的图 1a 中描述的抖动图:

I want to reproduce a jitterplot in R similar to the one described in Figure 1a of Zack et al., Nature Genetics, 2013:

我尝试了 beeswarm functino 和海盗功能.beeswarm 函数将点排成直线,它们看起来像一条线.我还尝试了 Pirateplot 函数,我通常喜欢它,但是,我没有弄清楚如何根据参考论文中的绘图中在 y 轴上的值来更改不同点的颜色.

I tried the beeswarm functino and the pirate function. The beeswarm function lines the points up to straight and they look like a line has been drawn. I also tried the Pirateplot function and I generally like it, however, I did not figure out how to change the color of different points based on their value on the y-axis as done in the plot from the reference paper.

最终,这些点应该像海盗图一样分散,但根据它们在 y 轴上的值进行颜色编码.

Eventually, the points should be scattered like in the pirate plot, but colorcoded according to their value on the y-axis.

大家有什么建议吗?

谢谢汤姆

推荐答案

我认为 ggplot2 是创建抖动图的最佳软件包.

I think ggplot2 is the best package to create a jitter plot.

ids <- c(
  rep("id1", 20), 
  rep("id2", 20), 
  rep("id3", 20)
)

values <- runif(60)

classes <- c(
  rep("class1", 30), 
  rep("class2", 30)
)

data <- data.frame(ids, values, classes)

library(ggplot2)

ggplot(data) +
  geom_jitter(
    aes(ids, values, color = classes), 
    width = 0.1
  )

这篇关于如何在 R 中生成与此类似的抖动图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆