kassambara / ggpubr

'ggplot2' Based Publication Ready Plots
https://rpkgs.datanovia.com/ggpubr/
1.12k stars 165 forks source link

Specify label.x positions #206

Closed brentocarrigan closed 5 years ago

brentocarrigan commented 5 years ago

Thank you kassambara for an outstanding package in ggpubr!

My question is similar to #50 and #190 but not a little different. If you wish to text label particular points with an ID, they seem to align incorrectly whether as a sole boxplot or facets. For example:

library(ggpubr)

set.seed(1234)
dat <- data.frame(donor = rep(c('HV017', 'HV018', 'HV019'), 6),
           day = rep(0:1, each = 9),
           dose = rep(c(20, 50, 100), each = 3),
           count = abs(rnorm(18)*10))

ggboxplot(data = dat,
          x = 'day',
          y = 'count',
       facet.by = 'dose',
          add = 'jitter',
       palette = 'npg',
       label = 'donor',
       label.select = list(top.up = 2))

In the faceted boxplot they seem to both be aligned left. The label.x.npc and/or label.y.npc arguments in stat_compare_means solves the similar problem of p-value positioning beautifully. Is there a similar fix when adding text labels?

kassambara commented 5 years ago

The problème is that the variable day is considered as numeric by the label function. You need to convert it as a factor variable before plotting.

Please, try this:

dat$day <- factor(dat$day, levels = c("0", "1"))
ggboxplot(data = dat,
          x = 'day',
          y = 'count',
          facet.by = 'dose',
          add = 'jitter',
          palette = 'npg',
          label = 'donor',
          label.select = list(top.up = 2))

The output should look like this:

Rplot

brentocarrigan commented 5 years ago

Outstanding, thank you! Problem solved