Matheus Barreto Alves
Matheus Barreto Alves

Reputation: 77

Adjust bin width with geom_col()

I'm having a trouble with the binwidth of my col graphics. I'm trying to show the highest/lowest suicide rate by country on the same page using shiny.

But, the country names are overlaping one another as you can see below: R

How can i adjust this?

Upvotes: 0

Views: 3189

Answers (2)

Joelle
Joelle

Reputation: 11

You can flip your graph horizontally, it will be better for the readers. You can add this to your graph to flip it:

  • coord_flip()

Upvotes: 1

chemdork123
chemdork123

Reputation: 13843

There are quite a few possibilities for resizing and adjusting things so that your long axis labels "fit" on a column plot (or any other ggplot for that matter). I'll go through some options here.

First of all... a sample dataset, since we did not get a suitable reprex from your question.

df <- data.frame(
  x=c('Text1', 'Text2', 'Long Text Here', 'Really Really Long Text Label Here', 'Text5', 'Text6', 'Text7'),
  y=c(sample(1:20, 7, replace=TRUE)))

df$x <- factor(df$x, levels=df$x)  # making sure ggplot doesn't alphabetically sort!
library(ggplot2)

p <- ggplot(df, aes(x,y)) +
  geom_col(aes(fill=x), show.legend = FALSE)
p

enter image description here

There's your overlapping labels. Now for some options:

Option #1: Resize the plot

One very simple method to "solving" the problem is to realize that R handles graphics... kind of funny. The look of a particular plot depends on the resolution and aspect ratio of the graphics device. Not only that, but text does not scale the same as the other plot elements. This means that you can fix the problem by forcing a different aspect ratio.

# this was used to create the above plot
ggsave('original.png', width=8, height=5)

# changing the aspect ratio produces the plot below on my graphics device
ggsave('resized.png', width=12, height=5)

enter image description here

Option #2: Change the Text Size

The other option is to make your text size for the axis labels smaller. The result is really similar to just resizing the plot.

p + theme(axis.text.x=element_text(size=6))

enter image description here

Option #3: Angle the Text

One really good option is to angle your text using the theme() element again. Note that when you do this you want to change the default alignment of your labels. Set hjust=1 so that the text is "right aligned". If you are setting your angle to 90°, you will also want to set your vjust=0.5 to make the text aligned with the tick mark vertically. Here I'll show you a 45° angled text option:

p + theme(axis.text.x=element_text(angle=45, hjust=1))

enter image description here

Option #4: Wrap Text Labels

One of my go-to favorite options here is to wrap the text label. There are a few ways to do this, but I prefer using wrap_format() from the scales package and a scale_* function. Note, the number given to wrap_format(X) indicates that wrapping should happen after X number of characters in the label.

library(scales)
p + scale_x_discrete(labels=wrap_format(22))

enter image description here

Option #5: Combine all Above

The best way to fix your problem is to use a combination of all techniques above to get the chart to look the way you believe looks most satisfying. This will depend on how many columns you have in your shiny plot and how you generate that plot (user input or always the same, etc). So it's up to you here.

p + scale_x_discrete(name=NULL, labels=wrap_format(22)) +
  scale_y_continuous(expand=expansion(mult=c(0,0.15))) +
  theme_classic() + #important to put this before overwriting individual theme elements!
  theme(
    axis.text.x=element_text(angle=40, hjust=1, size=15),
    axis.text.y=element_text(size=15),
    panel.grid.major.y=element_line(color='gray75', linetype=2))

enter image description here

Upvotes: 4

Related Questions