1

I have a geom_boxplot to which I would like to a second set of labels to each of the axis labels below the axis. In normal plots barplots (with plot), I would use the arguments:

par(xpd=T)
text(c("a", "b", "c", "d"), x=c(0.7, 1.9, 3.1, 4.3), 
        y=0, pos=1, offset=0.13)

Here's a sample graph using the mgp data showing the labels that I could want to add below the axis labels (but it is currently within the graph):

p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot()+
annotate("text", x = c(1:7), y=-10, label = paste(1:7*10, "mpg"), cex=3)

enter image description here

user3386170
  • 276
  • 4
  • 22
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Mar 29 '19 at 07:20

1 Answers1

2

Perhaps a simple way to to achieve this would be to create a new factor with levels containing info for both of the label sets.

library(ggplot2)

mpg %>%
  mutate(class = factor(class),
         newclass = factor(class, labels = paste0(levels(class), "\n", 1:7*10, " mpg" ))) %>%
  ggplot(aes(newclass, hwy))  +
  geom_boxplot()  

enter image description here

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56