How to change one specific facet in ggplot

2019-05-07 06:27发布

问题:

I created pie charts displaying different scores (0 - 100%) in ggplot with the help of facet_grid(). However the last score is a total score combining the other scores and in order to distinguish it better from the other scores I would like to change the parameters for this specific facet. Ideally, I would like to make the facet-label bold and move the facet a bit further away from the other facets, but I have no idea how to change the parameters of only one specific facet.

library(ggplot2)
df <- data.frame(label = c("A", "B", "Total"), score = c(60, 70, 65))

ggplot(df, aes(x = "", y = score)) + 
geom_bar(width = 1, stat = "identity") + 
coord_polar("y", start=0) + scale_y_continuous(limits = c(0, 100)) + 
facet_grid(. ~ label)

回答1:

1. Get the label done based on the link that @Richard posted

library(ggplot2)

df <- data.frame(label = c("A", "B", "Total"), score = c(60, 70, 65))

df$label2 <- factor(df$label, labels = c("A", "B", "bold(Total)"))

p1 <- ggplot(df, aes(x = "", y = score)) + 
  geom_bar(width = 1, stat = "identity") + 
  coord_polar("y", start=0) + scale_y_continuous(limits = c(0, 100)) + 
  facet_grid(. ~ label2, labeller = label_parsed)
p1

2. Modify the space between facets using gtable

library(grid)
library(gtable)

# create gtable object
gt = ggplot_gtable(ggplot_build(p1))

Check the layout

# gt$layout
# gt$layout$name
print(gt)

#> TableGrob (13 x 13) "layout": 23 grobs
#>     z         cells       name
#> 1   0 ( 1-13, 1-13) background
#> 2   1 ( 8- 8, 5- 5)  panel-1-1
#> 3   1 ( 8- 8, 7- 7)  panel-2-1
#> 4   1 ( 8- 8, 9- 9)  panel-3-1
#> 5   3 ( 6- 6, 5- 5)   axis-t-1
#> 6   3 ( 6- 6, 7- 7)   axis-t-2
#> 7   3 ( 6- 6, 9- 9)   axis-t-3
#> 8   3 ( 9- 9, 5- 5)   axis-b-1
#> 9   3 ( 9- 9, 7- 7)   axis-b-2
#> 10  3 ( 9- 9, 9- 9)   axis-b-3
#> 11  3 ( 8- 8, 4- 4)   axis-l-1
#> 12  3 ( 8- 8,10-10)   axis-r-1
#> 13  2 ( 7- 7, 5- 5)  strip-t-1
#> 14  2 ( 7- 7, 7- 7)  strip-t-2
#> 15  2 ( 7- 7, 9- 9)  strip-t-3
#> 16  4 ( 5- 5, 5- 9)     xlab-t
#> 17  5 (10-10, 5- 9)     xlab-b
#> 18  6 ( 8- 8, 3- 3)     ylab-l
#> 19  7 ( 8- 8,11-11)     ylab-r
#> 20  8 ( 4- 4, 5- 9)   subtitle
#> 21  9 ( 3- 3, 5- 9)      title
#> 22 10 (11-11, 5- 9)    caption
#> 23 11 ( 2- 2, 2- 2)        tag
#>                                             grob
#> 1                rect[plot.background..rect.121]
#> 2                        gTree[panel-1.gTree.29]
#> 3                        gTree[panel-2.gTree.46]
#> 4                        gTree[panel-3.gTree.63]
#> 5                                 zeroGrob[NULL]
#> 6                                 zeroGrob[NULL]
#> 7                                 zeroGrob[NULL]
#> 8             absoluteGrob[GRID.absoluteGrob.70]
#> 9             absoluteGrob[GRID.absoluteGrob.77]
#> 10            absoluteGrob[GRID.absoluteGrob.84]
#> 11            absoluteGrob[GRID.absoluteGrob.91]
#> 12                                zeroGrob[NULL]
#> 13                                 gtable[strip]
#> 14                                 gtable[strip]
#> 15                                 gtable[strip]
#> 16                                zeroGrob[NULL]
#> 17 titleGrob[axis.title.x.bottom..titleGrob.112]
#> 18   titleGrob[axis.title.y.left..titleGrob.115]
#> 19                                zeroGrob[NULL]
#> 20         zeroGrob[plot.subtitle..zeroGrob.117]
#> 21            zeroGrob[plot.title..zeroGrob.116]
#> 22          zeroGrob[plot.caption..zeroGrob.119]
#> 23              zeroGrob[plot.tag..zeroGrob.118]

Visualize the layout

library(lemon)
lemon::gtable_show_names(gt)

Check the name

names(gt)

#>  [1] "grobs"         "layout"        "widths"        "heights"      
#>  [5] "respect"       "rownames"      "colnames"      "name"         
#>  [9] "gp"            "vp"            "children"      "childrenOrder"

Take a look at the widths parameter. It shows that the space is 5.5pt in between each facet (1null).

gt$widths

#>  [1] 5.5pt               0cm                 1grobwidth         
#>  [4] 0.173972602739726cm 1null               5.5pt              
#>  [7] 1null               5.5pt               1null              
#> [10] 0cm                 0cm                 0pt                
#> [13] 5.5pt

We need to modify gt$widths[8] to increase the space between B and Total facets

gt$widths[8] = 3*gt$widths[8]

Check the result

grid.newpage()
grid.draw(gt)

Created on 2018-09-06 by the reprex package (v0.2.0.9000).