Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot多行和中心对齐x轴标签_R_Ggplot2 - Fatal编程技术网

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

R 使用ggplot多行和中心对齐x轴标签,r,ggplot2,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(an

我知道
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()
}

gg\u fun通常,您必须手动将换行符
'\n'
放置在标签中。但是,有人编写了一个自动执行此操作的函数,该函数在中提供。

您可以使用
stringr::str\u wrap
作为
scale\u x\u discrete
中的标签函数

让我们获取一些示例数据:

选项卡