控制facet_wrap ggplot2中的x标签

时间:2018-10-23 09:20:46

标签: r ggplot2

我有一个图,我希望第一个图的标签是 2018、19、20、21,另外两个分别为18、19、20、21。这可能吗?

示例

public static SecurityIdentifier usernameToSid(string user)
{
    if (user.StartsWith(@".\"))
    {
        user = user.Replace(@".\", Environment.MachineName + @"\");
    }

    NTAccount account = new NTAccount(user);
    return (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
}

1 个答案:

答案 0 :(得分:1)

这是一种方法

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)

使用gtable_show_names显示构面面板的名称。我们将看到x轴标签属于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]

从上面的打印输出中,我们知道axis-b-xxx属于grob#8至#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)

reprex package(v0.2.1.9000)于2018-10-23创建