r - R ggplot中的左对齐刻度线标签

标签 r ggplot2 visualization

我可以在 ggplot 中找到对齐图例和轴标签的选项,但不适用于刻度线标签。

是否可以让这些标签不与图形绘图框右对齐,而是与最长标签的开头或与整个图形边界的某个设定距离左对齐?

例子:

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 wrap
gg <- 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()

enter image description here

通缉:

enter image description here

最佳答案

由于它提供了解决方案,我将 @user20650 的评论作为答案。

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 wrap
gg <- 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()

我们开始
plot + theme(axis.text.y = element_text(hjust = 0))

left aligned y scale labels

关于r - R ggplot中的左对齐刻度线标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510653/

相关文章:

r - 整个过程中如何控制r中的小数点

r - 是否有 R 函数来获取预期计数表?

r - 如何使用 ggplot2 更改细粒度的颜色图例

elasticsearch - 是否可以从Kibana Visualization控件中删除/禁用值?

java - 为通用递归程序生成递归树的程序

python - 如何从数据列表制作直方图并使用 matplotlib 绘制它

r - 以下对象从 ‘package:stats’ :lowess 屏蔽

arrays - 逐行填充多维数组

r - ggplot2:绘制多边形和点 - 冲突的scale_color_manual

r - 折线图上的程序标签以避免线条 ggplot2