在ggpplot2中对齐轴文本

时间:2017-12-07 08:31:46

标签: r ggplot2

我使用ggplot2获得以下条形图。

enter image description here

我们可以观察到x轴文本没有与x轴对齐而是中心对齐。如何将所有x轴文本与x轴对齐? 我已经尝试更改axis.text.x = element_text(angle = 90, vjust = 1, hjust=1)中的值,如Align axis label on the right with ggplot2中所述,但无济于事。

用于绘图的代码如下:

 p <- ggplot(plot_solver,aes(x=name,round(value,2),fill=variable,label=round(value,2)))+ 
    geom_bar(stat="identity",width= 0.7)+
    theme_bw()+geom_text(size = 1, position = position_stack(vjust = 0.5))

    p + theme(legend.position=c(.3,0.85),legend.title = element_blank(),
    legend.text=element_text(size=8),legend.direction = 'horizontal',
    axis.title.y = element_text(size = 8,face = "bold"),
    axis.text.x = element_text(angle = 90, vjust = 1, hjust=1),
    axis.text.y = element_text(size = 9),axis.title.x=element_blank() )+
    guides(fill=guide_legend(title="Formats"))+
    scale_fill_manual(values=rainbow(4),labels=c("X ","Y  ","Z ","U "))+ 
    guides(fill=guide_legend(keywidth=0.2,keyheight=0.1,default.unit="inch",nrow=2))+
    ylab("Speedup")+theme(plot.margin=unit(c(1.0,0.6,0.6,0),"mm"))+ 
    theme(legend.key = element_blank())+
    theme(legend.background = element_rect(colour = "black"))+
    theme(panel.border = element_rect(colour = "black"))

下面给出了数据结构的dput()以再现数据结构:

> dput(plot_solver)
structure(list(name = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L), .Label = c("12month1        ", 
"amazon0312      ", "Andrews         ", "av41092         ", "bundle_adj     ", 
"bundle1         ", "flickr          ", "Freescale1      ", "Ga3As3H12       ", 
"hollywood-2009  ", "in-2004         ", "lhr71c          ", "mouse_gene     ", 
"nemeth23        ", "rail4284        ", "RM07R           ", "SiO2            ", 
"spal_004       ", "thermal2        ", "thermomech_TK  ", "torso1          ", 
"TSOPF_RS_b2383", "wb-edu          ", "Zd_Jac2        "), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("solver_csr_50", 
    "solver_hyb_50", "solver_csr_250", "solver_hyb_250"), class = "factor"), 
    value = c(1.483853636, 0.833971405, 0.947560492, 2.403142882, 
    1.626413472, 0.991467865, 2.538000882, 1.316118676, 1.221777675, 
    1.078077417, 1.664918578, 1.212409967, 1.364104634, 1.017229312, 
    3.149256956, 1.12104879, 1.242688247, 0.82382667, 1.118503136, 
    1.008526996, 2.228076048, 1.068948907, 1.884906941, 1.105205313, 
    1.120927241, 0.641889919, 1.046822614, 1.468693059, 1.500747131, 
    1.25410977, 1.059699706, 0.910956693, 1.43968056, 1.397810123, 
    1.094151621, 1.182516419, 1.626171249, 1.397388864, 2.440610373, 
    1.035590769, 1.216158929, 1.257113662, 0.800255725, 0.962159854, 
    2.946661991, 2.237199908, 0.96889509, 1.363652596, 1.483905263, 
    0.893797519, 1.029405415, 2.471348689, 1.671715929, 1.00715098, 
    2.69514063, 1.455418078, 1.235602809, 1.087707209, 1.763360364, 
    1.250831883, 1.368235001, 1.028816823, 3.155078988, 1.131893099, 
    1.268342707, 0.824145645, 1.215626973, 1.066510807, 2.300424627, 
    1.075502897, 2.115832988, 1.105855239, 1.120966241, 0.687936797, 
    1.137241238, 1.510377387, 1.542549251, 1.273947374, 1.125310773, 
    1.007373319, 1.455971394, 1.410295887, 1.158845619, 1.219990993, 
    1.631095127, 1.413306866, 2.44512233, 1.045608412, 1.24126571, 
    1.257600401, 0.869744941, 1.017477853, 3.042344008, 2.250916734, 
    1.087597561, 1.364454504)), row.names = c(NA, -96L), .Names = c("name", 
"variable", "value"), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

name变量有空格。

library(stringr)    
df$name <- str_replace_all(df$name, "\\s", '')