【问题标题】:move axis labels ggplot移动轴标签ggplot
【发布时间】:2011-12-10 03:50:39
【问题描述】:

我在 ggplot2 中生成了一个事实图,并且 x 轴标题(底部)略微触及比例值(当我绘制到 .pdf 设备时它变得更糟)。如何将轴标题向下移动一点?

DF<-structure(list(race = structure(c(3L, 1L, 3L, 2L, 3L, 1L, 2L, 
2L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 2L, 1L, 2L, 3L), .Label = c("asian", 
"black", "white"), class = "factor"), gender = structure(c(1L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 
2L, 2L, 2L), .Label = c("female", "male"), class = "factor"), 
    score = c(0.0360497844302483, 0.149771418578119, 0.703017688328021, 
    1.32540102136392, 0.627084455719946, -0.320051801571444, 
    0.852281028633536, -0.440056896755573, 0.621765489966213, 
    0.58981396944136, 1.95257757882381, 0.127301498272644, -0.0906338578670778, 
    -0.637727808028146, -0.449607617033673, 1.03162398117388, 
    0.334259623567608, 0.0912327543652576, -0.0789977852804991, 
    0.511696466039959), time1 = c(75.9849658266583, 38.7148843859919, 
    54.3512613852158, 37.3210772390582, 83.8061071736856, 14.3853324033061, 
    79.2285735003004, 31.1324602891428, 22.2294730114138, 26.427263191766, 
    40.5529893144888, 19.2463281412667, 8.45085646487301, 97.6770352620696, 
    61.1874163107771, 31.3727683430548, 99.4155144857594, 79.0996849438957, 
    21.2504885323517, 94.1079332400361)), .Names = c("race", 
"gender", "score", "time1"), class = "data.frame", row.names = c(NA, 
-20L))


require(ggplot2)
p <- ggplot(DF, aes(score, time1, group=gender))
p + geom_point(aes(shape=19)) + facet_grid(race~gender) + scale_x_continuous('BLAH BLAH') + 
scale_y_continuous('Some MOre Of theat Good Blahing') 

在我的数据中,BLAH BLAH 正在触及数字。我需要它向下移动。怎么样?

【问题讨论】:

  • 嗯。您能否扩展一下它的外观或发布示例图像,因为当我运行它时它看起来很好。可能特定于平台/设备/图像大小。
  • @joran 我无法发布图像或数据,因为我正在使用其他人的数据。虽然我无法在这个数据集之外复制问题,所以我尽我所能。
  • 没问题...我看到你得到了调整对齐的指针。我只是很感兴趣,因为我无法复制它。

标签: r ggplot2


【解决方案1】:

您可以使用以下方法调整 x 轴标题的位置:

+ opts(axis.title.x = theme_text(vjust=-0.5))

使用 -0.5“垂直对齐”参数,直到它适合您/您的显示设备。

【讨论】:

  • 记住 opts 和 theme_text 这些天已被弃用。你应该使用 theme(axis.title.x = element_text(vjust=-0.5))
  • 是否有一个等价物可以水平改变定位,同时保留文本的排列? hjust 似乎更改了文本对齐方式,我只想更改位置。
  • 至少从 ggplot 2.0.0 开始,vjust 参数已被margins 取代。可以为顶部 (t)、右侧 (r)、底部 (b) 和左侧 (l) 指定边距,以垂直和水平放置文本。因此,要将文本向下移动,您可以使用 theme(axis.title.x = element_text(margin = margin(t = 20))
【解决方案2】:

这是一个简单的解决方法,基于here提供的答案

只需添加一个换行符; \n,在坐标区标题的开头; xlab("\nYour_x_Label")(如果您需要移动 y 标签,也可以在最后)。

它提供的控制不如 Eduardo 在 cmets 中的建议那么多; theme(axis.title.x = element_text(vjust=-0.5)),也可以使用margin,不过简单多了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2014-07-20
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多