How to draw boxplot with each point and background color by R ?
1
0
Entering edit mode
4.2 years ago
takoyaki ▴ 120

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.

R RNA-Seq • 6.9k views
ADD COMMENT
3
Entering edit mode
m = data.frame( "A"=c(1,2,3), "B"=c(2,3,4) )
boxplot(m , xlab="Sample", ylab="value")
stripchart(list("A"=m$A, "B"=m$B), vertical=TRUE, method="jitter", col=c("red","blue"), cex=1.8, add=T, pch=16)

Rplot

ADD REPLY
0
Entering edit mode

Thanks! That is what I wanted to draw !

ADD REPLY
1
Entering edit mode

Like the following example? enter image description here

ADD REPLY
0
Entering edit mode

no, background is "points" background. points in A should have magenta color and points in B should have cyan color.

ADD REPLY
1
Entering edit mode
ADD REPLY
0
Entering edit mode

now I tried to draw boxplot by default boxplot. but your comment is correct, so thanks

ADD REPLY
0
Entering edit mode

Issue is resolved?

ADD REPLY
0
Entering edit mode

well, this is not resolved, but I'm giving up :)

ADD REPLY
0
Entering edit mode

I see... I have posted the answer below.

ADD REPLY
10
Entering edit mode
4.2 years ago

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')

ddd

Kevin

ADD COMMENT
1
Entering edit mode

Thank you for long code ! I'm going to use as reference code. ggplot2 is so powerful.

ADD REPLY
1
Entering edit mode

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 ;)

ADD REPLY
1
Entering edit mode

your codes are lifesaver

ADD REPLY

Login before adding your answer.

Traffic: 2769 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6