How to create box plots with all points where for each group, the color of the points can be assigned manually

Viewed 92

I have a data frame:

> dput(df2)
structure(list(Genotype = c("miR-15/16 FL", "miR-15/16 FL", "miR-15/16 FL", 
"miR-15/16 FL", "miR-15/16 FL", "miR-15/16 cKO", "miR-15/16 cKO", 
"miR-15/16 cKO", "miR-15/16 cKO", "miR-15/16 cKO"),
`Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127` = c(1191L, 1325L, 1089L, 1154L, 1147L, 1735L, 1441L, 1455L, 1560L, 1623L)),
.Names = c("Genotype", "Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127"),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
MFI=c(1191,1325,1089,1154,1147,1735,1441,1455,1560,1623))

and I would like to make a boxplot with each point plotted for each group (miR-15/16 FL and miR-15/16 cKO) and have all the

'miR-15/16 FL' 

points be closed black circles and all the

'miR-15/16 cKO' 

points be open red circles. I would like to be able to manually adjust the colors and shapes/sizes of the points for each group.

So far, I have tried:

library(ggplot2)
ggplot(data=df2, aes(x = df2$Genotype, y = df2[2])) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(position = position_jitter(width = .2), shape=1, size=5) +
ylim(0,max(df2[2])+10)

But I haven't been able to figure out how to adjust the colors/shapes independently for

'miR-15/16 FL'

and

'miR-15/16 cKO'

Thanks for any help with this!

2 Answers

This may get you started:

ggplot(data=df2, aes(x = Genotype, y = MFI)) +
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(aes(col = Genotype, shape = Genotype),position = position_jitter(width = .2), size=5) +
  ylim(0,max(df2$MFI)+10)+
  scale_shape_manual(values = c(1,16))+
  scale_color_manual(values = c('red', 'black'))

I've found this site very useful: http://sape.inf.usi.ch/quick-reference/ggplot2/shape

@Balter beat me to the punch.... The only difference is that I set the color and shape parameters outside the ggplot call for future progamatic access.

library(ggplot2)

df2 <- data.frame(Genotype = c('WT','WT','WT','WT','WT',
                               'cKO','cKO','cKO','cKO','cKO'),
                  MFI=c(1191,1325,1089,1154,1147,1735,1441,1455,1560,1623))

color.groups <- c(WT="black", cKO="red")
shape.groups <- c(WT=20, cKO=21)

ggplot(data=df2, aes(x = df2$Genotype, y = df2$MFI)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitter(width = .2), size=5, 
             aes(color=Genotype, shape = Genotype)) +
  ylim(0,max(df2$MFI)+10) + 
  scale_color_manual(values=color.groups) +
  scale_shape_manual(values=shape.groups)

Update:

library(ggplot2)

df2 <- data.frame(Genotype = c('miR-15/16 WT','miR-15/16 WT','miR-15/16 WT','miR-15/16 WT','miR-15/16 WT',
                               'miR-15/16 cKO','miR-15/16 cKO','miR-15/16 cKO','miR-15/16 cKO','miR-15/16 cKO'),
                  MFI=c(1191,1325,1089,1154,1147,1735,1441,1455,1560,1623))

color.groups <- c(`miR-15/16 WT`="black", `miR-15/16 cKO`="red")
shape.groups <- c(`miR-15/16 WT`=20, `miR-15/16 cKO`=21)

ggplot(data=df2, aes(x = Genotype, y = MFI)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitter(width = .2), size=5, 
             aes(color=Genotype, shape = Genotype)) +
  ylim(0,max(df2$MFI)+10) + 
  scale_color_manual(values=color.groups) +
  scale_shape_manual(values=shape.groups)

enter image description here

Update2:

df2 <- structure(list(Genotype = c("miR-15/16 FL", "miR-15/16 FL", "miR-15/16 FL", 
                                   "miR-15/16 FL", "miR-15/16 FL", "miR-15/16 cKO", "miR-15/16 cKO", 
                                   "miR-15/16 cKO", "miR-15/16 cKO", "miR-15/16 cKO"),
                      `Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127` = c(1191L, 1325L, 1089L, 1154L, 1147L, 1735L, 1441L, 1455L, 1560L, 1623L)),
                 .Names = c("Genotype", "Cells/SC/Live/CD8—,, CD4+/Foxp3-,Median,<BV421-A>,CD127"),
                 row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
colnames(df2) <- c("Genotype", "MFI")

color.groups <- c("black","red")
names(color.groups) <- unique(df2$Genotype)
shape.groups <- c(20, 21)
names(shape.groups) <- unique(df2$Genotype)

ggplot(data=df2, aes(x = Genotype, y = MFI)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitter(width = .2), size=5, 
             aes(color=Genotype, shape = Genotype)) +
  ylim(0,max(df2$MFI)+10) + 
  scale_color_manual(values=color.groups) +
  scale_shape_manual(values=shape.groups)

enter image description here

Related