使用 ggplot 多线和中心对齐 x 轴标签

hkj*_*447 3 r ggplot2

我知道hjust用于 x标题轴,但是我将如何对 x 轴标签进行居中和多线处理?这是我的绘图功能:

gg_fun<-function(){
  ggplot(tab,                  
         aes(x = Var1, y = Percent)) +
    #theme_light() +
    theme(panel.background = element_rect(fill = NA),
          axis.title.y=element_text(angle=0, vjust=0.5, face="bold"), 
          axis.title.x=element_blank(),
          axis.text.y = element_text(size = 10), 
          axis.text.x = element_text(size = 12),
          axis.ticks.x = element_blank(),
          axis.ticks.y = element_blank(),
          #panel.grid.minor = element_line(colour = "dark gray"),
          panel.grid.major.x = element_blank() ,
          # explicitly set the horizontal lines (or they will disappear too)
          panel.grid.major.y = element_line(size=.1, color="dark gray" ),
          axis.line = element_line(size=.1, colour = "black"),
          plot.background = element_rect(colour = "black",size = 1)) +
    geom_bar(stat = "Identity", fill="#5596E6") + #"cornflower" blue
    ggtitle(element_blank()) + 
    scale_y_continuous(expand =  c(0, 0), breaks = round(seq(0, 1, by = .1), digits = 2), 
                       labels  = percent(round(seq(0, 1, by = .1), digits = 2), digits = 0),
                       limits = c(0,.6)) #+
    #scale_x_discrete()
}
Run Code Online (Sandbox Code Playgroud)

这是它生成的示例图: 在此处输入图片说明

我知道对于 的n.dodge争论scale_x_discrete(),但这不是我要找的。我也不想简单地缩写使用labels = abbreviate或精确指定,因为这很耗时。我也看过例如levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect)),但这会跳过每一行并使一些标签太长。我将如何将 x 标签文本居中并使其多行以防止重叠?我要去的例子:

在此处输入图片说明

All*_*ron 5

您可以将其stringr::str_wrap用作标签功能scale_x_discrete.

让我们取一些示例数据:

tab <- data.frame(Var1 = c("Video of presentation incl visuals",
                          "Video of presentation, written text and visuals",
                          "Written text, plus visuals",
                          "Other (please specify)"),
                 Percent = c(0.33, 0.34, 0.16, 0.17))
Run Code Online (Sandbox Code Playgroud)

使用您的原始函数,这给出了以下图:

gg_fun()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但经过以下修改:

gg_fun<-function(){
  ggplot(tab,                  
         aes(x = Var1, y = Percent)) +
    #theme_light() +
    theme(panel.background = element_rect(fill = NA),
          axis.title.y=element_text(angle=0, vjust=0.5, face="bold"), 
          axis.title.x=element_blank(),
          axis.text.y = element_text(size = 10), 
          axis.text.x = element_text(size = 12),
          axis.ticks.x = element_blank(),
          axis.ticks.y = element_blank(),
          #panel.grid.minor = element_line(colour = "dark gray"),
          panel.grid.major.x = element_blank() ,
          # explicitly set the horizontal lines (or they will disappear too)
          panel.grid.major.y = element_line(size=.1, color="dark gray" ),
          axis.line = element_line(size=.1, colour = "black"),
          plot.background = element_rect(colour = "black",size = 1)) +
    geom_bar(stat = "Identity", fill="#5596E6") + #"cornflower" blue
    ggtitle(element_blank()) + 
    scale_y_continuous(expand =  c(0, 0), 
                       breaks = round(seq(0, 1, by = .1), digits = 2), 
                       labels  = scales::percent(round(seq(0, 1, by = .1), 
                                                       digits = 2), digits = 0),
                       limits = c(0,.6)) +
    scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 16))
}
Run Code Online (Sandbox Code Playgroud)

我们得到:

gg_fun()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明