我正在使用的代码大致如下:
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)
Run Code Online (Sandbox Code Playgroud)
除了条形图点的颜色之外,我从中获得的几乎是相同的
my_image http://is.muni.cz/de/256262/Rplot.png
我应该如何编辑我的代码以重现正确的着色?我以为
bg=bpColor[2:3][melt$variable]
Run Code Online (Sandbox Code Playgroud)
会做这个工作,但是我得到这个输出,如果我要删除[]括号我有两种颜色,但在组内混合.谢谢你的帮助.
不是最优雅的方式,但嘿,它的工作
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)
Run Code Online (Sandbox Code Playgroud)

这应该是一个简短的评论,但它有点太大了.我不回答你的问题,但我希望提供的行为的一些见解col和bg中stripchart.
我注意到两件事似乎可以解释你的问题(1)col和bg参数中的颜色以不同的方式"分配"到这些点.该col颜色用于行明智的,而bg颜色分配给点逐列.(2)只从颜色矢量中挑选一行(col颜色)或列(bg颜色)中所需的颜色,然后再循环它们.分配和回收规则一起bg意味着将bg颜色映射到不同级别是很棘手的x.
# a very simple data set to make it easier to see what's going on
y <- rep(1:3, 3)
x <- rep(c("a", "b", "c"), each = 3)
Run Code Online (Sandbox Code Playgroud)
col颜色按行使用,而bg颜色按列使用
stripchart(y ~ x, pch = 21,
col = c("red", "orange", "yellow"),
bg = rep(c("white", "grey", "black")),
vertical = TRUE, cex = 4, lwd = 5)
Run Code Online (Sandbox Code Playgroud)
仅使用前三种col颜色.然后他们重新循环
stripchart(y ~ x, pch = 21,
col = c("red", "orange", "yellow",
"green", "blue", "purple",
"white", "grey", "black"),
bg = rep(c("white", "grey", "black")),
vertical = TRUE, cex = 4, lwd = 5)`
Run Code Online (Sandbox Code Playgroud)
仅使用前三种bg颜色.然后他们重新循环.因此,'不可能'将bg颜色' 映射到x(分组变量)
stripchart(y ~ x, pch = 21,
col = c("red", "orange", "yellow"),
bg = c("white", "grey", "black",
"red", "orange", "yellow",
"green", "blue", "purple"),
vertical = TRUE, cex = 4, lwd = 5)
Run Code Online (Sandbox Code Playgroud)
只是进一步尝试:
stripchart(y ~ x, pch = 21,
col = c("red", "orange", "yellow"),
bg = rep(c("white", "grey", "black"), 3),
vertical = TRUE, cex = 4, lwd = 5)
stripchart(y ~ x, pch = 21,
col = c("red", "orange", "yellow"),
bg = rep(c("white", "grey", "black")),
vertical = TRUE, cex = 4, lwd = 5)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4580 次 |
| 最近记录: |