Home > Blockchain >  ggplot box plot by group with labels on each box
ggplot box plot by group with labels on each box

Time:11-17

I am trying to make a grouped boxplot with an annotation showing the number of rows in each group but I can't seem to get it to work

I've created this example to illustrate the problem

#define variables
bin=rep(c('A', 'B', 'C'), each=50)
category=rep(c('small', 'large'), each=25)
val=seq(1:150) sample(1:100, 100, replace=TRUE)

#create dataset using variables
df=data.frame(bin, category, val)

It has 150 rows divided into bins and categories

#view top rows of dataframe
head(df)

  bin category val
1   A    small  93
2   A    small 100
3   A    small  16
4   A    small  66
5   A    small   9
6   A    small  69

I use tidyverse to make a dataframe with the annotation information

library(tidyverse)

# count bin and category
countBinCat <-df %>%
  group_by(bin, category) %>%
  summarize(count= sum(!is.na(val)))


# make the data for the annotations
box_annot <- data.frame(
  x = c(0.8, 1.2, 1.8, 2.2, 2.8, 3.2),
  y = c(10, 10, 10, 10, 10, 10),
  label = c(
    countBinCat$count[1], 
    countBinCat$count[2],
    countBinCat$count[3],
    countBinCat$count[4],
    countBinCat$count[5],
    countBinCat$count[6]
  )
)

Using all of this I can make a grouped boxplot or have labels but I get an error if I try to have both:

# Chart 1 
# works with groups but no label
ggplot(df, aes(x=bin, y=val, fill=category))   
  geom_boxplot()

# Chart 2 -
# works with labels but no groups
ggplot(df, aes(x=bin, y=val))   
  geom_boxplot()   
  geom_text(data=box_annot, aes(label=paste('n=',label,sep=''), y = y, x=x))

#Chart 3 - 
# fails with both
ggplot(df, aes(x=bin, y=val, fill=category))   
  geom_boxplot()   
  geom_text(data=box_annot, aes(label=paste('n=',label,sep=''), y = y, x=x))

Charts 1 and 2 work the third one gives the following error: Error in `check_aesthetics()`: ! Aesthetics must be either length 1 or the same as the data (6): x and fill

How can I get both groups and labels on the chart?

CodePudding user response:

In chart 3 just move the fill = category inside the geom_boxplot() and it should work.

ggplot(df, aes(x=bin, y=val))   
  geom_boxplot(aes(fill=category))   
  geom_text(data=box_annot, aes(label=paste('n=',label,sep=''), y = y, x = x))

enter image description here

  • Related