MLEN
MLEN

Reputation: 2561

Controlling x-labels in facet_wrap ggplot2

I have a plot where I would like the labels for the first plot to be 2018, 19, 20, 21 and for the other two 18, 19, 20, 21. Is this possible?

Example

library(ggplot2)

df <- structure(list(date = structure(c(17532, 17897, 18262, 18628, 
                      17532, 17897, 18262, 18628, 17532, 17897, 18262, 18628), class = "Date"), 
                     var = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 
                     3L, 3L), .Label = c("a", "b", "c"), class = "factor"), value = c(-1.09243979230319, 
                    1.61989681830401, 0.0814424200676222, -0.408078030248257, 
                    -0.904237506821678, 0.787027860989217, 0.706295579996191, 
                    -0.223410122960503, 0.0764830701612793, 0.927863681429729, 
                    -0.56458790218662, -0.954306740205979)), class = "data.frame", row.names = c(NA, -12L))

ggplot(df, aes(x = date, y = value)) +
  geom_bar(stat = 'identity') +
  scale_x_date(breaks = unique(df$date), labels = c(2018, 19:21)) +
  facet_wrap(~var)

Upvotes: 1

Views: 200

Answers (1)

Tung
Tung

Reputation: 28441

Here is one way to do it

library(dplyr)
library(ggplot2)

df <- df %>% 
  mutate(date = as.Date(date))

p1 <- ggplot(df, aes(x = date, y = value)) +
  geom_bar(stat = 'identity') +
  scale_x_date(date_labels = "%y") +
  facet_wrap(~ var)

library(grid)
library(lemon)

Use gtable_show_names to display the names of the facet panels. We will see that the x-axis labels belong to axis-b-xxx

gtable_show_names(p1)

# Generate a ggplot2 plot grob.
gt <- ggplotGrob(p1)
print(gt)
#> TableGrob (13 x 17) "layout": 27 grobs
#>     z         cells        name
#> 1   0 ( 1-13, 1-17)  background
#> 2   1 ( 8- 8, 5- 5)   panel-1-1
#> 3   1 ( 8- 8, 9- 9)   panel-2-1
#> 4   1 ( 8- 8,13-13)   panel-3-1
#> 5   3 ( 6- 6, 5- 5)  axis-t-1-1
#> 6   3 ( 6- 6, 9- 9)  axis-t-2-1
#> 7   3 ( 6- 6,13-13)  axis-t-3-1
#> 8   3 ( 9- 9, 5- 5)  axis-b-1-1
#> 9   3 ( 9- 9, 9- 9)  axis-b-2-1
#> 10  3 ( 9- 9,13-13)  axis-b-3-1
#> 11  3 ( 8- 8,12-12)  axis-l-1-3
#> 12  3 ( 8- 8, 8- 8)  axis-l-1-2
#> 13  3 ( 8- 8, 4- 4)  axis-l-1-1
#> 14  3 ( 8- 8,14-14)  axis-r-1-3
#> 15  3 ( 8- 8,10-10)  axis-r-1-2
#> 16  3 ( 8- 8, 6- 6)  axis-r-1-1
#> 17  2 ( 7- 7, 5- 5) strip-t-1-1
#> 18  2 ( 7- 7, 9- 9) strip-t-2-1
#> 19  2 ( 7- 7,13-13) strip-t-3-1
#> 20  4 ( 5- 5, 5-13)      xlab-t
#> 21  5 (10-10, 5-13)      xlab-b
#> 22  6 ( 8- 8, 3- 3)      ylab-l
#> 23  7 ( 8- 8,15-15)      ylab-r
#> 24  8 ( 4- 4, 5-13)    subtitle
#> 25  9 ( 3- 3, 5-13)       title
#> 26 10 (11-11, 5-13)     caption
#> 27 11 ( 2- 2, 2- 2)         tag
#>                                             grob
#> 1                rect[plot.background..rect.409]
#> 2                       gTree[panel-1.gTree.271]
#> 3                       gTree[panel-2.gTree.286]
#> 4                       gTree[panel-3.gTree.301]
#> 5                                 zeroGrob[NULL]
#> 6                                 zeroGrob[NULL]
#> 7                                 zeroGrob[NULL]
#> 8            absoluteGrob[GRID.absoluteGrob.308]
#> 9            absoluteGrob[GRID.absoluteGrob.308]
#> 10           absoluteGrob[GRID.absoluteGrob.308]
#> 11                                zeroGrob[NULL]
#> 12                                zeroGrob[NULL]
#> 13           absoluteGrob[GRID.absoluteGrob.329]
#> 14                                zeroGrob[NULL]
#> 15                                zeroGrob[NULL]
#> 16                                zeroGrob[NULL]
#> 17                                 gtable[strip]
#> 18                                 gtable[strip]
#> 19                                 gtable[strip]
#> 20                                zeroGrob[NULL]
#> 21 titleGrob[axis.title.x.bottom..titleGrob.400]
#> 22   titleGrob[axis.title.y.left..titleGrob.403]
#> 23                                zeroGrob[NULL]
#> 24         zeroGrob[plot.subtitle..zeroGrob.405]
#> 25            zeroGrob[plot.title..zeroGrob.404]
#> 26          zeroGrob[plot.caption..zeroGrob.407]
#> 27              zeroGrob[plot.tag..zeroGrob.406]

From the print output above, we know that the axis-b-xxx belong to grob #8 to #10

# Change the label of the 1st facet
gt$grobs[[8]]$children[[2]]$grobs[[2]]$children[[1]]$label <- c(2018, 19:21)
# We can even remove the label of the 2nd facet
gt$grobs[[9]]$children[[2]]$grobs[[2]]$children[[1]]$label <- ""

grid.newpage()
grid.draw(gt)

Created on 2018-10-23 by the reprex package (v0.2.1.9000)

Upvotes: 1

Related Questions