Quantcast
Channel: Left-adjust (hjust = 0) vertical x axis labels on facets with free scale - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by Edward for Left-adjust (hjust = 0) vertical x axis labels on facets with free scale

$
0
0

I tried with empty padding to the maximum character length, but this doesn't result in the same length for all strings.

This caught my attention. Actually, it would result in the same length for all strings if you padded the labels with spaces, made them all the same length, and ensured the font family was non-proportionally spaced.

First, pad the labels with spaces such that all labels have the same length. I'm going to ustilise the str_pad function from the stringr package.

library(ggplot2)data("diamonds")diamonds$cut_label <- paste("Super Dee-Duper", as.character(diamonds$cut))library(stringr)diamonds$cut_label <- str_pad(diamonds$cut_label, side="right",                              width=max(nchar(diamonds$cut_label)), pad="")

Then, you may need to load a non-proportionally-spaced font using the extrafont package.

library(extrafont)font_import(pattern='consola')  # Or any other of your choice.

Then, run the ggplot command and specify a proportionally spaced font using the family argument.

ggplot(data = diamonds, aes(cut_label, carat)) +  facet_grid(~cut, scales = "free_x") +  theme(axis.text.x = element_text(angle = 90, family="Consolas"))

enter image description here


Viewing all articles
Browse latest Browse all 5

Trending Articles