如何证明R ggplot中的文本轴标签

时间:2022-09-27 20:11:43

I have a bar chart with text labels along the x-axis. Some of the labels are quite lengthy and I would like to make them look neater. Any ideas of how I might achieve that?

我有一个条形图,沿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))

3 个解决方案

#1


11  

Like this?

如何证明R ggplot中的文本轴标签

Since you didn't provide any data, we have no way of knowing what your attempt looks like, but this seems like it might be close. The main feature is the use of strwrap(...) to insert CR (\n) into your labels.

由于您没有提供任何数据,我们无法知道您的尝试是什么样的,但这似乎可能很接近。主要功能是使用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()

#2


1  

Rotating the axis labels can help a lot:

旋转轴标签可以提供很多帮助:

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

#3


1  

You may change the breakLabelsAt parameter, decrease the axisLabelSize and set flipCoordinates to TRUE, then you get similar results. I used the efc-sample data set which is included in the sjPlot-package:

您可以更改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)

如何证明R ggplot中的文本轴标签

#1


11  

Like this?

如何证明R ggplot中的文本轴标签

Since you didn't provide any data, we have no way of knowing what your attempt looks like, but this seems like it might be close. The main feature is the use of strwrap(...) to insert CR (\n) into your labels.

由于您没有提供任何数据,我们无法知道您的尝试是什么样的,但这似乎可能很接近。主要功能是使用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()

#2


1  

Rotating the axis labels can help a lot:

旋转轴标签可以提供很多帮助:

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

#3


1  

You may change the breakLabelsAt parameter, decrease the axisLabelSize and set flipCoordinates to TRUE, then you get similar results. I used the efc-sample data set which is included in the sjPlot-package:

您可以更改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)

如何证明R ggplot中的文本轴标签