R ggplot:使用绘图中的点数更新镶嵌面标签

R ggplot:使用绘图中的点数更新镶嵌面标签,r,ggplot2,facet,facet-wrap,R,Ggplot2,Facet,Facet Wrap,我有一个数据集数据,可以根据以下要求进行绘图: #Data data <- data.frame(level = c(1,2,3,5,2,4,3,1,3), pay1 = c(10,21,32,12,41,21,36,14,17), pay2 = c(26,36,5,6,52,12,18,17,19)) #Plot data %>% pivot_longer(-level) %>% ggplo

我有一个数据集
数据
,可以根据以下要求进行绘图:

#Data
data <- data.frame(level = c(1,2,3,5,2,4,3,1,3), 
                   pay1 = c(10,21,32,12,41,21,36,14,17), 
                   pay2 = c(26,36,5,6,52,12,18,17,19))

#Plot
data %>% pivot_longer(-level) %>%
  ggplot(aes(x = name, y = value))+
  geom_boxplot() + 
  facet_wrap(~ level, scales = "free_x") +
  theme_bw()

如何将刻面标签更新为
1(n=2)
2(n=2)
3(n=3)
4(n=1)
5(n=1)

我只需使用
粘贴
n>的
调用来重命名因子级别:

数据%>%
枢轴长度(-level)%>%
分组依据(级别、名称)%>%
突变(级别=粘贴0(级别,“(n=”,n(),”))%>%
ggplot(aes(x=名称,y=值))+
geom_箱线图()+
面_包裹(~level,scales=“free_x”)+
主题_bw()

table(data$level)
# 1 2 3 4 5 
# 2 2 3 1 1