【问题标题】:Left-align tick mark labels in R ggplotR ggplot中的左对齐刻度线标签
【发布时间】:2015-08-11 04:52:18
【问题描述】:

我可以在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()


通缉:

【问题讨论】:

标签: r ggplot2 visualization


【解决方案1】:

因为它提供了解决方案,所以我将@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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2012-10-06
    • 1970-01-01
    • 2021-02-18
    • 2020-09-04
    相关资源
    最近更新 更多