【问题标题】:How to justify text axis labels in R ggplot如何在 R ggplot 中证明文本轴标签的合理性
【发布时间】:2014-02-24 02:33:30
【问题描述】:

我有一个带有沿 x 轴的文本标签的条形图。有些标签很长,我想让它们看起来更整洁。关于我如何实现这一目标的任何想法?

library(sjPlot)
require(ggplot2)
require(ggthemes)
WAM_3_plot <- sjp.frq(WAM_Dec13_R2$WAM_3, title= c("WAM Item 3"),
    axisLabels.x=c("Disruptive behaviour can be contained and does not spread to other patients.  Generally, behaviour on the ward is positive and pro-therapeutic.", 
                   "1", "2","3","4",
                   "Disruptive behaviour by one patient tends to spread to other patients and is only contained with great difficulty. The general level of behaviour seems to be getting more counter-therapeutic."),
    barColor = c("palegreen4", "palegreen3", "palegreen2", "brown1", "brown2", "brown3"),
    upperYlim = 25,
    valueLabelSize = 5,
    axisLabelSize = 1.2,
    breakLabelsAt=14, returnPlot=TRUE) 
WAM_3_plot + theme(axis.text.x=element_text(hjust=0.5))

【问题讨论】:

    标签: r ggplot2 axis-labels justify


    【解决方案1】:

    像这样?

    由于您没有提供任何数据,我们无法知道您的尝试是什么样的,但这似乎很接近。主要功能是使用strwrap(...) 将CR (\n) 插入到您的标签中。

    set.seed(1)
    library(ggplot2)
    axisLabels.x <- c("Disruptive behaviour can be contained and does not spread to other patients.  Generally, behaviour on the ward is positive and pro-therapeutic.", 
                   "1", "2","3","4",
                   "Disruptive behaviour by one patient tends to spread to other patients and is only contained with great difficulty. The general level of behaviour seems to be getting more counter-therapeutic.")
    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()
    

    【讨论】:

    • 不客气。由于您是 SO 新手,this link 解释了当有人回答您的问题时该怎么做。惯例是在接受答案之前等待 24 小时,以防其他人提出更好的答案。
    【解决方案2】:

    旋转轴标签会有很大帮助:

    theme(axis.text.x  = element_text(angle=90, vjust=0.5))
    

    【讨论】:

    • 感谢您的建议,但在这种情况下不起作用,标签上的文字太长了!
    • 我刚看到你的标签“破坏性行为可以被控制,不会传播给其他患者。一般来说,病房里的行为是积极的,有利于治疗的。”你确定你根本不能缩短这个吗?我觉得尝试重新格式化以使其适合是相当无望的。
    • 是的,在我看来,如果标签在 lhs 上显示为 (Note 1)(Note 2) 之类的内容,并且在包含长文本。
    【解决方案3】:

    您可以更改breakLabelsAt 参数,减小axisLabelSize 并将flipCoordinates 设置为TRUE,然后您会得到类似的结果。我使用了 sjPlot-package 中包含的 efc-sample 数据集:

    data(efc)
    sjp.frq(efc$e42dep, title=c("WAM Item 3"),
        axisLabels.x=c("Disruptive behaviour can be contained and does not spread to other patients.  Generally, behaviour on the ward is positive and pro-therapeutic.", 
          "1", "2",
          "Disruptive behaviour by one patient tends to spread to other patients and is only contained with great difficulty. The general level of behaviour seems to be getting more counter-therapeutic."),
        valueLabelSize=5,
        axisLabelSize=.8,
        breakLabelsAt=50,
        flipCoordinates=T)
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多