如何在ggplot中更改一个特定的方面 [英] How to change one specific facet in ggplot

查看:159
本文介绍了如何在ggplot中更改一个特定的方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我借助facet_grid()创建了在ggplot中显示不同分数(0-100%)的饼图.但是,最后一个分数是结合其他分数的总分数,为了更好地将其与其他分数区分开,我想更改此特定方面的参数.理想情况下,我想使构面标签为粗体,并使构面与其他构面之间的距离稍远一些,但我不知道如何仅更改一个特定构面的参数.

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.根据链接,完成标签制作. @Richard发布的

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.使用 gtable 修改构面之间的空间strong>

2. Modify the space between facets using gtable

library(grid)
library(gtable)

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

检查布局

# 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]

可视化布局

library(lemon)
lemon::gtable_show_names(gt)

检查姓名

names(gt)

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

看看widths参数.它显示每个构面(1null)之间的空间为5.5pt.

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

我们需要修改gt $ widths [8]来增加BTotal构面之间的空间

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

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

检查结果

grid.newpage()
grid.draw(gt)

reprex软件包(v0.2.0.9000)创建于2018-09-06.

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

这篇关于如何在ggplot中更改一个特定的方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆