Here is how you can change the colour of the box-and-whiskers, and also the overlaid points.
Note the pairings for the colouring:
- Box-and-whisker plot:
geom_boxplot()
and scale_fill_manual()
- Scatter plot:
geom_jitter()
and scale_color_manual()
.
df <- data.frame(
Group = c(rep('B-cell\nlymphocytes',50), rep('Natural\nkiller cells', 50)),
log2(matrix(rexp(200, rate=.1), ncol=2)))
colnames(df) <- c('Group', 'Gene1', 'Gene2')
require(reshape2)
require(ggplot2)
df <- melt(df, id.vars = c('Group'))
colnames(df) <- c('Group', 'Gene', 'Expression')
ggplot(data = df, aes(x = Group, y = Expression)) +
geom_boxplot(
position = position_dodge(width=0.5),
outlier.shape = 17,
outlier.colour = 'red2',
outlier.size = 0.1,
aes(fill = Group)) +
scale_fill_manual(values = c( 'red', 'royalblue')) + # for boxplot
#Add the scatter points (treats outliers same as 'inliers')
geom_jitter(
position = position_jitter(width = 0.3),
size = 3.0,
aes(colour = Group)) +
scale_color_manual(values = c('pink', 'skyblue')) + # for scatter plot dots
# facet by Gene
facet_wrap(~ Gene, ncol = 2) +
#Set the size of the plotting window
theme_bw(base_size=24) +
#Modify various aspects of the plot text and legend
theme(
legend.position = 'none',
legend.background = element_rect(),
plot.title = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.subtitle = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
plot.caption = element_text(angle = 0, size = 16, face = 'bold', vjust = 1),
axis.text.x = element_text(angle = 45, size = 16, face = 'bold', hjust = 1.10),
axis.text.y = element_text(angle = 0, size = 16, face = 'bold', vjust = 0.5),
axis.title = element_text(size = 16, face = 'bold'),
axis.title.x = element_text(size = 16, face = 'bold'),
axis.title.y = element_text(size = 16, face = 'bold'),
#Legend
legend.key = element_blank(), #removes the border
legend.key.size = unit(1, 'cm'), #Sets overall area/size of the legend
legend.text = element_text(size = 14, face = 'bold'), #Text size
title=element_text(size = 14, face = 'bold'),
#facet wrap
strip.text.x = element_text(size = 14, face = 'bold'),
strip.text.y = element_text(size = 14, face = 'bold', angle=0),
strip.background = element_rect(fill = 'white'),
strip.text = element_text(size = 14, face = 'bold', colour = 'black'))+
#Change the size of the icons/symbols in the legend
guides(colour = guide_legend(override.aes = list(size = 2.5))) +
#Set x- and y-axes labels
xlab('Immune cell class') +
ylab('Expression') +
labs(title = 'Good morning',
subtitle = 'Guten morgen',
caption = 'Buongiorno')
Kevin
Thanks! That is what I wanted to draw !
Like the following example?
no, background is "points" background. points in A should have magenta color and points in B should have cyan color.
Boxplot in ggplot2
now I tried to draw boxplot by default boxplot. but your comment is correct, so thanks
Issue is resolved?
well, this is not resolved, but I'm giving up :)
I see... I have posted the answer below.