Reputation: 45
Below is my code to plot a box plot in ggplot2.I want to group the box plots by a type,so that all types are grouped together and colored.But I am still getting it mixed up.
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))
Do I need to add anything in my code above.For reference,below is my data.
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
Thanks
Upvotes: 1
Views: 404
Reputation: 54277
As I commented:
library(ggplot2)
ggplot(brcp.df,
aes(x = variable,
y = value)) +
geom_boxplot(aes(fill=factor(type)),
position = position_dodge(width = .8))
produces boxes of each type (here just 2) side by side:
I used
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")
Upvotes: 1