绘制在R中按颜色分组的箱形图

时间:2015-04-28 15:41:07

标签: r ggplot2

下面是我在ggplot2中绘制框图的代码。我想按类型对框图进行分组,以便所有类型都组合在一起并着色。但我仍然把它混淆了。

p=ggplot(brcp.df,aes(variable,value)) + geom_point(shape=1) 
p + geom_boxplot(aes(fill=factor(type))) + geom_point(aes(color = factor(type)),outlier.colour="red") +geom_text(aes(label = gene) , size = 4,position = position_jitter(width = 0.6))

我是否需要在上面的代码中添加任何内容。作为参考,下面是我的数据。

 variable gene      value diagnosis     type      lQntl       uQntl     lBound    uBound
1 PM169_Z7 <NA> -0.1220775      CRPC Prostate -0.3693246 -0.02714948 -0.8825873 0.4861132
2 PM169_Z7 <NA> -0.4711975      CRPC Prostate -0.3693246 -0.02714948 -0.8825873 0.4861132
3 PM169_Z7 <NA> -0.5884106      CRPC Prostate -0.3693246 -0.02714948 -0.8825873 0.4861132

由于

1 个答案:

答案 0 :(得分:1)

正如我评论的那样:

library(ggplot2)
ggplot(brcp.df,
       aes(x = variable, 
           y = value)) + 
  geom_boxplot(aes(fill=factor(type)), 
               position = position_dodge(width = .8)) 

并排生成每种类型的盒子(这里只是2个):

enter image description here

我用过

brcp.df <- read.table(header=T, text="
variable gene      value diagnosis     type      lQntl       uQntl     lBound    uBound
1 PM169_Z7 <NA> -0.1220775      CRPC Prostate -0.3693246 -0.02714948 -0.8825873 0.4861132
2 PM169_Z7 <NA> -0.4711975      CRPC Prostate -0.3693246 -0.02714948 -0.8825873 0.4861132
3 PM169_Z7 <NA> -0.5884106      CRPC Prostate -0.3693246 -0.02714948 -0.8825873 0.4861132
4 PM169_Z7 <NA> -0.1220775      CRPC Prostate2 -0.3693246 -0.02714948 -0.8825873 0.4861132
5 PM169_Z7 <NA> -0.4711975      CRPC Prostate2 -0.3693246 -0.02714948 -0.8825873 0.4861132
6 PM169_Z7 <NA> -0.5884106      CRPC Prostate2 -0.3693246 -0.02714948 -0.8825873 0.4861132")