【问题标题】:Alignment of Axis text in ggpplot2ggplot2中轴文本的对齐方式
【发布时间】:2017-12-07 08:31:45
【问题描述】:

我有以下使用 ggplot2 的条形图。

我们可以观察到 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")

【问题讨论】:

标签: r ggplot2


【解决方案1】:

name 变量有空格。

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

【讨论】:

  • 我不知道我是怎么错过的。可能是因为 View() 不显示空格
  • @Stormvirux 是的,View() 在 RStudio 中不显示空格。我看不到View() 是如何工作的,也许它会自动删除空格。
猜你喜欢
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多