R语言 移动轴标签ggplot

pieyvz9o  于 2023-01-18  发布在  其他
关注(0)|答案(3)|浏览(251)

我在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')

在我的数据中,布拉赫布拉赫正在触摸数字。我需要它向下移动。怎么做?

llmtgqce

llmtgqce1#

可以使用以下命令调整x轴标题的位置:

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

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

cetgtptt

cetgtptt2#

根据提供的答案here,这是一个简单的解决方法
只需添加一个换行符;\n,位于坐标轴标题的开头;xlab("\nYour_x_Label")(如果需要移动y标签,则在末尾)。
它没有像Eduardo在评论中建议的那样提供更多的控制;theme(axis.title.x = element_text(vjust=-0.5)),或者使用margin,但要简单得多!

t98cgbkg

t98cgbkg3#

我想指出的是,这不是我的答案,而是@JWilliman -他们的答案在@Prasad Chalasani答案的评论中。我写这篇文章是因为目前投票赞成的答案实际上对我来说并不奏效,但@JWilliman解决方案确实有效:

#Answer   
+ theme(axis.title.x = element_text(margin = margin(t = 20))

这是因为theme(axis.title.x = element_text(vjust = 0.5))已被取代,现在将标题/标签移动固定距离,而不管您输入的值是什么。
请赞成JWilliman的评论-如果他们张贴的解决方案作为答案,那么我会删除这个!

相关问题