11

I have an issue where I have overlapping axis labels and can't seem to get a solution to fix this.

enter image description here

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

Is there a way to fix this so that there are no overlaps?

coder_007
  • 187
  • 1
  • 2
  • 9
  • 4
    This has now been fixed in the latest version of ggplot2 (ver 3.3.0). Can do, for example p + scale_x_discrete(guide = guide_axis(n.dodge = 2)) – andyyy Mar 17 '20 at 16:14

1 Answers1

21

You can tune a bit your x axis either by automatically abbreviating labels as in

p + scale_x_discrete(labels = abbreviate)

or you can provide abbreviated labels yourself as in

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

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

ƒacu.-
  • 507
  • 3
  • 9
  • 12
    This is a good answer, but another personal favorite is to alternate a newline character at the beginning of every xlabel: `p + scale_x_discrete( labels = function( labels ) { fixedLabels <- c() for ( l in 1:length( labels ) ) { fixedLabels <- c( fixedLabels, paste0( ifelse( l %% 2 == 0, '', '\n' ), labels[l] ) ) } return( fixedLabels ) } )` – aaiezza Apr 21 '17 at 17:34