Thank you for long code ! I'm going to use as reference code. ggplot2 is so powerful.
Hey everyone.
I failed to draw boxplot with each point by this R scripts and I want to know why.
m = data.frame( "A"=c(1,2,3), "B"=c(2,3,4) )
boxplot(m , xlab="Sample", ylab="value")
stripchart( m , vertical=TRUE, method="jitter", pch=c(21,22), col=c("red","blue"), bg=c("magenta","cyan"), cex=1.8, add=T)
From this script, I could draw boxplot and point itself, but background color is not my expectation.
I wanted to set background color magenta in group "A" and cyan in group "B".
Does anyone know why ? and how to draw like my expectation ?
Thanks.
1 answer
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()andscale_fill_manual() - Scatter plot:
geom_jitter()andscale_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
Indeed. I have been building up this code for a few years, adding to it each time. I now just copy-paste it when I need to generate plots like this. Happy to share it with you. In fact, that version (above) is a bit old, and I have already added more functionality to it ;)
your codes are lifesaver
Log in to answer this question.

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.
Thanks! That is what I wanted to draw !