Jas Jas - 2 months ago 18
R Question

Follow up on x-axis labels: Stagger axis labels, new feature in ggplot2

Stagger axis labels, new feature in ggplot2

I'm new at R and following the answer of @Sandy Muspratt to @spindoctor is a bit daunting. The answer works great for the y-axis labels. I tried some editing. For example I changed:

index <- which(g$layout$name == "axis-l") # Which grob


to:

index <- which(g$layout$name == "axis-b") # Which grob


But the x-axis labels remained as they were, unstaggered. Could you please indicate how to modify the code so that it works for the x-axis labels as well?

# Get the grob
g <- ggplotGrob(out.plot)

# Get the y axis
index <- which(g$layout$name == "axis-l") # Which grob
yaxis <- g$grobs[[index]]

# Get the ticks (labels and marks)
ticks <- yaxis$children[[2]]

# Get the labels
ticksL <- ticks$grobs[[1]]

# Make the edit
ticksL$children[[1]]$x <- rep(unit.c(unit(c(1,0,-1),"npc")), 27)

# Put the edited labels back into the plot
ticks$grobs[[1]] <- ticksL
yaxis$children[[2]] <- ticks
g$grobs[[index]] <- yaxis

# Make the relevant column a little wider
g$widths[3] <- unit(2.5, "cm")

# Draw the plot
grid.newpage()
grid.draw(g)


The output of the TableGrob is as follows:

>g
TableGrob (6 x 5) "layout": 8 grobs
z cells name grob
1 0 (1-6,1-5) background rect[plot.background..rect.507]
2 3 (3-3,3-3) axis-l absoluteGrob[GRID.absoluteGrob.498]
3 1 (4-4,3-3) spacer zeroGrob[NULL]
4 2 (3-3,4-4) panel gTree[GRID.gTree.484]
5 4 (4-4,4-4) axis-b absoluteGrob[GRID.absoluteGrob.491]
6 5 (5-5,4-4) xlab titleGrob[axis.title.x..titleGrob.501]
7 6 (3-3,2-2) ylab titleGrob[axis.title.y..titleGrob.504]
8 7 (2-2,4-4) title zeroGrob[plot.title..zeroGrob.505]


I tried identifying relevant x,y-axis values but navigating this structure is a bit foreign to me. Any suggestions or comments or resources to avoid time wasted guessing are greatly appreciated.

Answer

You just need to change height not width in this case. Also in editGrob you need to pass the y slot not the x since you are changing the coordinates wrt the y-axis not left to right on the x.

I kept everything the same but commented out the things I changed and put my changes directly under.

# Libraries
library(ggplot2)
library(gtable)
library(grid)
library(stringi)

# fake data
set.seed(12345)
var <- stri_rand_strings(81, 4, pattern = '[HrhEgeIdiFtf]')
var1 <- rnorm(81, mean = 175, sd = 75)
out <- data.frame(var, var1)
out$var <- factor(out$var, levels = out$var[order(out$var1, decreasing = FALSE)])

# Plot
# out.plot <- ggplot(out, aes(x = var, y = var1)) + geom_point() + coord_flip()
out.plot <- ggplot(out, aes(x = var1, y = var)) + geom_point() + coord_flip()

# Get the ggplot grob
g = ggplotGrob(out.plot)

# Get a hierarchical list of component grobs
grid.ls(grid.force(g))

# make the relevant column a little wider
# g$widths[3] = unit(2.5, "cm")
g$heights[4] = unit(1, "cm")

# The edit
g = editGrob(grid.force(g),
             gPath("axis-b", "axis", "axis", "GRID.text"),
             # x = unit(c(-1, 0, 1), "npc"), 
             y = unit(c(1, 0, -1), "npc"), ## or c(-1,0,1) etc to change order
             grep = TRUE)

# Draw the plot
grid.newpage()
grid.draw(g)

enter image description here

Comments