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
157 views
in Technique[技术] by (71.8m points)

plot - How to generate a jitterplot similar to this one in R?

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

Plot of interest

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.

enter image description here

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

Anyone any suggestions?

Thanks Tom

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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
  )

ggplot2 jitter example


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

...