本文介绍了左对齐R ggplot中的刻度标记标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我可以在 ggplot 中找到对齐图例和轴标签的选项,但不能打勾记号标签。 是否有可能使这些标签不与图形绘图框右对齐,而是左对齐与最长标签的开始位置齐平,还是与整个图形边框有一定的距离? 示例: set.seed(1)库(ggplot2 ) axisLabels.x< - c(这是一个较长的标签,短标签,短标签,短标签,短标签,这又是一个较长的标签) labels.wrap )#word wrap gg ggplot(gg)+ geom_bar(aes(x,y,fill = x),stat =identity)+ scale_x_discrete(labels = labels.wrap)+ scale_fill_discrete(guide =none)+ labs(x =,y = Response)+ coord_flip() 通缉: 解决方案由于它提供了解决方案,我使@ user20650的评论成为答案。 > set.seed(1) library(ggplot2) axisLabels.x< - c(This is更长的标签,短标签,短标签,短标签,短标签,这又是一个较长的标签) labels.wrap< ; - lapply(strwrap(axisLabels.x,50,simplified = F),paste,collapse =\\\)#word wrap# gg< - data.frame(x = LETTERS [1:6] ,y = sample(1:10,6)) plot geom_bar(aes(x,y,fill = x),stat =identity)+ scale_x_discrete(labels = labels.wrap)+ scale_fill_discrete(guide =none)+ labs(x =,y =Response)+ coord_flip() plot + theme(axis.text.y = element_text(hjust = 0)) I can find options to align the legend and axis labels in ggplot, but not for the tick mark labels.Is it possible to have those labels not right-aligned to the graph plot-box, but left-aligned flush with either the start of the longest label, or some set distance from the overall graph-border?Example:set.seed(1)library(ggplot2)axisLabels.x <- c("This is a longer label", "Short label", "Short label","Short label","Short label", "This is again a longer label")labels.wrap <- lapply(strwrap(axisLabels.x,50,simplify=F),paste,collapse="\n") # word wrapgg <- data.frame(x=LETTERS[1:6], y=sample(1:10,6))ggplot(gg) + geom_bar(aes(x,y, fill=x), stat="identity")+ scale_x_discrete(labels=labels.wrap)+ scale_fill_discrete(guide="none")+ labs(x="",y="Response")+ coord_flip()Wanted: 解决方案 As it provides the solution, I make @user20650's comment an answer.set.seed(1)library(ggplot2)axisLabels.x <- c("This is a longer label", "Short label", "Short label","Short label","Short label", "This is again a longer label")labels.wrap <- lapply(strwrap(axisLabels.x,50,simplify=F),paste,collapse="\n") # word wrapgg <- data.frame(x=LETTERS[1:6], y=sample(1:10,6))plot <- ggplot(gg) + geom_bar(aes(x,y, fill=x), stat="identity")+ scale_x_discrete(labels=labels.wrap)+ scale_fill_discrete(guide="none")+ labs(x="",y="Response")+ coord_flip()And here we goplot + theme(axis.text.y = element_text(hjust = 0)) 这篇关于左对齐R ggplot中的刻度标记标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 22:55