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

r - Point color (col) and fill color (bg) by group in stripchart

I'm trying to reproduce the following image image http://www.davidzeleny.net/wiki/lib/exe/fetch.php/vizualizace:figures:boxplots-jitter-rdbu-colors.png?cache=

The code I'm using is roughly this:

library(RColorBrewer) 
library(reshape2)

a=rnorm(100, mean=1)
b=rnorm(100, mean=0, sd=1)
ab=data.frame(a,b)
melt=melt(ab)
bpColor=brewer.pal(4, 'RdBu')

boxplot(melt$value ~ melt$variable, notch=T, col=c(bpColor[1], bpColor[4]), outline=F, varwidth=T)
stripchart(melt$value ~ melt$variable, add=T, vertical=T, pch=21,
         bg=bpColor[2:3][melt$variable], method='jitter', jitter=0.02)

What I'm getting from this is almost the same except for the color of the stripchart points

my_image http://is.muni.cz/de/256262/Rplot.png

How should I edit my code in order to reproduce the proper coloring? I thought, that

bg=bpColor[2:3][melt$variable]

would do the job, however I'm getting this output, if I would erase the [] brackets I got two colors, but mixed within the groups. Thank you advance for your help.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Not the most elegant way, but hey, it's working

boxplot(melt$value ~ melt$variable, notch=T, col=c(bpColor[1], bpColor[4]), outline=F, varwidth=T)
stripchart(melt[melt$variable == "a", "value"] ~ melt[melt$variable == "a", "variable"], add=T, vertical=T, pch=21, bg=c(bpColor[2]), method='jitter', jitter=0.02)
stripchart(melt[melt$variable == "b", "value"] ~ melt[melt$variable == "b", "variable"], add=T, vertical=T, pch=21, bg=c(bpColor[3]), method='jitter', jitter=0.02)

enter image description here


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

...