Skip to content

Instantly share code, notes, and snippets.

@baptiste
Last active December 7, 2016 21:18
Show Gist options
  • Save baptiste/079e987215b1bfa0c245e2e03ae07f31 to your computer and use it in GitHub Desktop.
Save baptiste/079e987215b1bfa0c245e2e03ae07f31 to your computer and use it in GitHub Desktop.
library(ggplot2)
library(grid)
my_axis <- function(low="low", high="high", axis=c("x", "y"), ...){
axis <- match.arg(axis)
if(axis == "x"){
g1 <- textGrob(low, x=unit(0,"npc"), hjust=0)
g3 <- textGrob(high, x=unit(1,"npc"), hjust=1)
g2 <- segmentsGrob(grobWidth(g1) + unit(2,"mm"), unit(0.5,"npc"),
unit(1,"npc") - grobWidth(g3)- unit(2,"mm"),
unit(0.5,"npc"), ...)
} else if(axis == "y"){
g1 <- textGrob(low, y=unit(0,"npc"), rot=90, hjust=0)
g3 <- textGrob(high, y=unit(1,"npc"), rot=90, hjust=1)
g2 <- segmentsGrob(unit(0.5,"npc"),grobHeight(g1) + unit(2,"mm"),
unit(0.5,"npc"),
unit(1,"npc") - grobHeight(g3)- unit(2,"mm"),
...)
}
grobTree(g1,g2,g3)
}
element_grob.custom_axis <- function(element, ...) {
my_axis(axis=element$axis,arrow=arrow(length=unit(2,"mm")))
}
## silly wrapper to fool ggplot2
axis_custom <- function(axis = "x"){
structure(
list(axis = axis),
class = c("custom_axis","element_blank", "element") # inheritance test workaround
)
}
theme_arrow <- function (base_size = 11, base_family = "")
{
theme_grey(base_size = base_size, base_family = base_family) %+replace%
theme(axis.line.x = axis_custom(axis="x"),
axis.line.y=axis_custom(axis="y"),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.ticks.length = unit(1,"line"))
}
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
facet_wrap(~Species)+
geom_line() +
theme_arrow()
@baptiste
Copy link
Author

baptiste commented Dec 1, 2016

screen shot 2016-12-01 at 7 09 04 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment