Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create abbreviated legends manually for long X labels in ggplot2

Tags:

r

legend

ggplot2

I would like to create a simple bar chart with ggplot2 and my problem is that my x variable contains long strings so the labels are overlaid.

Here are fake datas and the plot :

library(dplyr)
library(tidyr)
library(ggplot2)

set.seed(42)
datas <- data.frame(label = sprintf("aLongLabel%d", 1:8), 
           ok = sample(seq(0, 1, by = 0.1), 8, rep = TRUE)) %>% 
  mutate(err = abs(ok - 1)) %>% 
  gather(type, freq, ok, err)

datas %>% 
  ggplot(aes(x = label, y = freq)) + 
  geom_bar(aes(fill = type), stat = "identity")

enter image description here

I would like to replace the labels by shorter ones and create a legend to show the matches.

What I've tried :

I use the shape aes parameter in geo_point which will create a legend with shapes (and plots shapes that I hide with alpha = 0). Then I change the shapes with scale_shape_manual and replace the x labels with scale_x_discrete. With guides I override the alpha parameter of my shapes so they wont be invisible in the legend.

leg.txt <- levels(datas$label)
x.labels <- structure(LETTERS[seq_along(leg.txt)], 
                      .Names = leg.txt)

datas %>% 
  ggplot(aes(x = label, y = freq)) + 
  geom_bar(aes(fill = type), stat = "identity") + 
  geom_point(aes(shape = label), alpha = 0) + 
  scale_shape_manual(name = "Labels", values = x.labels) + 
  guides(shape = guide_legend(override.aes = list(size = 5, alpha = 1))) + 
  scale_x_discrete(name = "Label", labels = x.labels)

enter image description here

It gives me the expected output but I feel like this is very hacky.

Does ggplot2 provides a way to do this more directly ? Thanks.

like image 764
Julien Navarre Avatar asked Jun 03 '15 07:06

Julien Navarre


1 Answers

Rotation solution suggested by Pascal

Rotate the labels and align them to the edge :

datas %>% 
  ggplot(aes(x = label, y = freq)) + 
  geom_bar(aes(fill = type), stat = "identity") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

like image 67
Julien Navarre Avatar answered Nov 14 '22 05:11

Julien Navarre