【问题标题】:Overlapping axis labels in R [duplicate]R中的重叠轴标签[重复]
【发布时间】:2013-11-03 06:45:36
【问题描述】:

我遇到了一个问题,我的轴标签重叠,并且似乎无法找到解决此问题的解决方案。

p <- ggplot(data=Data,aes(x=Indicator,y=Numeric,group=Expenditure_group,shape=Expenditure_group,colour=Expenditure_group))+geom_point()+geom_line()

有没有办法解决这个问题,以免重叠?

【问题讨论】:

  • 这已在最新版本的 ggplot2(版本 3.3.0)中得到修复。可以做,比如p + scale_x_discrete(guide = guide_axis(n.dodge = 2))

标签: r ggplot2 linegraph


【解决方案1】:

您可以通过自动缩写标签来调整 x 轴,如

p + scale_x_discrete(labels = abbreviate)

或者您可以自己提供缩写标签,如

p + scale_x_discrete(labels = c("Congenital Rubella" = "C. Rub.", ..., "Total tetanus" = "T. tet.", "Yellow fever" = "Y. fever")

见:http://docs.ggplot2.org/current/scale_discrete.html

【讨论】:

  • 这是一个很好的答案,但另一个个人最喜欢的是在每个 xlabel 的开头替换一个换行符:p + scale_x_discrete( labels = function( labels ) { fixedLabels &lt;- c() for ( l in 1:length( labels ) ) { fixedLabels &lt;- c( fixedLabels, paste0( ifelse( l %% 2 == 0, '', '\n' ), labels[l] ) ) } return( fixedLabels ) } )
猜你喜欢
  • 2011-10-10
  • 2012-10-25
  • 2019-10-01
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
相关资源
最近更新 更多