2

I use the following commond to generate pairwise boxplot. The ctrp.boxdata looks like:

        drug       auc            p    group
1 paclitaxel 0.2077276 0.0004955335 High PPS
2 paclitaxel 0.1772445 0.0004955335 High PPS
3 paclitaxel 0.1599633 0.0004955335 High PPS
4 paclitaxel 0.1564113 0.0004955335 High PPS
5 paclitaxel 0.1737403 0.0004955335 High PPS
6 paclitaxel 0.2429842 0.0004955335 High PPS
...
           drug       auc            p   group
139 clofarabine 0.4589299 0.0002743925 Low PPS
140 clofarabine 0.3600058 0.0002743925 Low PPS
141 clofarabine 0.4433972 0.0002743925 Low PPS
142 clofarabine 0.3785587 0.0002743925 Low PPS
143 clofarabine 0.3744288 0.0002743925 Low PPS
144 clofarabine 0.3954452 0.0002743925 Low PPS
p <- ggplot(ctrp.boxdata, aes(drug, auc, fill=group)) + 
  geom_boxplot(aes(col = group),outlier.shape = NA) + 
  geom_text(aes(drug, y=min(ctrp.boxdata$auc) * 1.1, 
                label=paste("p = ",formatC(p,format = "e",digits = 1))),
            data=ctrp.boxdata, 
            inherit.aes=F) + 
  scale_fill_manual(values = c("#0772B9","#48C8EF")) + 
  scale_color_manual(values = c("#0772B9","#48C8EF")) + 
  xlab(NULL) + ylab("Estimated AUC value")

I want to change the color of median line and I followed this thread:

dat <- ggplot_build(p)$data[[1]]
p + geom_segment(data=dat, aes(x=xmin, xend=xmax, y=middle, yend=middle), color="white", size=2)

but it got me an error:

Error: Continuous value supplied to discrete scale

Any solutions?

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Sugus
  • 59
  • 6

1 Answers1

3

The issue here is that geom_segment() inherits aesthetics from the ggplot object in p which has discrete scales for color and fill. To circumvent this, set inherit.aes = FALSE – just as you do in geom_text() above:

p + geom_segment(data = dat, 
                 aes(x = xmin, xend = xmax, y = middle, yend = middle), 
                 color = "white", inherit.aes = F)

Note that I've removed size = 2 because such a thick line is not very helpful when you want to show the median.

Using the data you provided, the result looks like this:

enter image description here

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22