2017-02-25 525 views
0

我目前正在做一个箱形图与叠加点。 boxplot中的点必须根据因子变量在颜色上有所不同。代码顺利,直到在这里:如何使用不同颜色的叠加点创建箱形图?

MioBox <- ggplot(mydata, aes(x=mng, y=Active, fill=mng))+ 
    geom_boxplot(color="black", notch=TRUE)+ 
    geom_point(position="jitter", color="blue", alpha=.5)+ 
    geom_rug(side="l", color="black")+ 
    facet_grid(.~hor,scales = "free", space = "free")+ 
    labs(title='bla bla bla') 

(见下图)

当我尝试根据变量plot

MioBox <- ggplot(mydata, aes(x=mng, y=Active, fill=mng))+ 
geom_boxplot(color="black", notch=TRUE)+ 
#geom_point(position="jitter", color="blue", alpha=.5)+ 
geom_rug(side="l", color="black")+ 
facet_grid(.~hor,scales = "free", space = "free")+ 
labs(title='bla bla bla') 
MioBox + scale_fill_manual(values=c("#669966", "#CC9966", "#CCCC66"))+ 
geom_point(position="jitter",aes(color = factor(mydata$plot))) 

我得到的箱线图以箱线图的颜色添加到分这与每个盒图中每个颜色分配3个点的变量图不匹配。结果是我一直有9点,但大多数时候有一种颜色和三个其他的,6至有一种颜色缺失: superimposed points coloured by factor variable (there should be 3 dots per color in each boxplot)

这里是能重现问题的表格:

mydata <- read.table(header=TRUE, text=" 
Active hor plot mng 
7.20 F 1 CH 
8.80 O 1 CH 
9.30 F 1 CH 
9.20 O 1 CH 
9.70 F 1 CH 
9.30 O 1 CH 
9.10 F 2 CH 
7.50 O 2 CH 
7.50 F 2 CH 
8.70 O 2 CH 
9.90 F 2 CH 
7.60 O 2 CH 
9.70 F 3 CH 
7.70 O 3 CH 
8.90 F 3 CH 
8.60 O 3 CH 
8.30 F 3 CH 
8.30 O 3 CH 
8.50 L 1 CH 
7.40 L 1 CH 
8.00 L 1 CH 
9.70 L 2 CH 
8.90 L 2 CH 
8.40 L 2 CH 
9.80 L 3 CH 
8.00 L 3 CH 
7.00 L 3 CH 
7.30 F 1 Fe 
6.60 O 1 Fe 
6.50 F 1 Fe 
6.60 O 1 Fe 
6.90 F 1 Fe 
5.80 O 1 Fe 
6.60 F 2 Fe 
7.00 O 2 Fe 
6.00 F 2 Fe 
5.10 O 2 Fe 
6.10 F 2 Fe 
5.10 O 2 Fe 
5.10 F 3 Fe 
6.50 O 3 Fe 
7.70 F 3 Fe 
6.90 O 3 Fe 
5.20 F 3 Fe 
6.30 O 3 Fe 
6.50 L 1 Fe 
5.00 L 1 Fe 
7.80 L 1 Fe 
5.10 L 2 Fe 
5.50 L 2 Fe 
5.60 L 2 Fe 
5.50 L 3 Fe 
7.80 L 3 Fe 
7.70 L 3 Fe 
7.20 F 1 W 
8.80 O 1 W 
7.80 F 1 W 
7.80 O 1 W 
7.90 F 1 W 
8.10 O 1 W 
8.60 F 2 W 
7.40 O 2 W 
7.40 F 2 W 
8.40 O 2 W 
7.70 F 2 W 
8.90 O 2 W 
6.70 F 3 W 
6.10 O 3 W 
7.50 F 3 W 
8.60 O 3 W 
7.80 F 3 W 
8.60 O 3 W 
8.30 L 1 W 
8.20 L 1 W 
8.70 L 1 W 
8.60 L 2 W 
6.80 L 2 W 
6.30 L 2 W 
7.30 L 3 W 
7.10 L 3 W 
7.70 L 3 W 
") 

任何人都可以帮助我?

+0

你可以添加数据的一个子集,所以我们可以重现该问题:

您可以通过重写你的最后几行这样的解决?看到建议[这里](http://meta.stackexchange.com/questions/176460/how-to-paste-data-from-r-to-stackoverflow)关于你可能想怎么做:) –

+0

非常感谢科琳,我希望上面的表格有帮助吗? BAlpine – BAlpine

+0

科琳娜我刚刚发现它与该表上面的工作,但是当我的变量为“hor”添加不同的价值观这是行不通的。 – BAlpine

回答

0

问题在于您拨打mydata$plot时应该只拨打plot。 ggplot对象已经有了一个预定义的data frame

MioBox + 
scale_fill_manual(values=c("#669966", "#CC9966", "#CCCC66"))+ 
geom_point(position="jitter",aes(color = factor(plot))) 
+0

非常感谢cimentadaj,我知道了异常值的双重点。另外,我想用特定颜色填充箱图。我的问题是,如果你观察箱线图,应该有每色3个点(在根据总到变量“HOR”三种颜色),用于每一个9个的盒状图。事实上,大多数时候,一个颜色有六个点,另一个颜色有三个,但缺少一种颜色。 @cimentadaj – BAlpine

+0

是的,对不起,我误会了,但我想通了你的错误。您需要从'geom_point'调用中清除'mydata $'。只要离开'plot'即可。我会编辑答案。 – cimentadaj

+0

非常感谢cimentadaj,它很棒! – BAlpine

相关问题