You can create a custom element function for axis.text.x
, but it's quite fiddly and convoluted. Similar requests have been made in the past, it would be nice to have a clean solution for this and other custom changes (strip labels, axes, etc.) Feature request, anyone?
library(jpeg)
img <- lapply(list.files(pattern="jpg"), readJPEG )
names(img) <- c("Anaphase", "Interphase", "Metaphase", "Prophase", "Telophase")
require(ggplot2)
require(grid)
# user-level interface to the element grob
my_axis = function(img) {
structure(
list(img=img),
class = c("element_custom","element_blank", "element") # inheritance test workaround
)
}
# returns a gTree with two children: the text label, and a rasterGrob below
element_grob.element_custom <- function(element, x,...) {
stopifnot(length(x) == length(element$img))
tag <- names(element$img)
# add vertical padding to leave space
g1 <- textGrob(paste0(tag, "
"), x=x,vjust=0.6)
g2 <- mapply(rasterGrob, x=x, image = element$img[tag],
MoreArgs = list(vjust=0.7,interpolate=FALSE,
height=unit(5,"lines")),
SIMPLIFY = FALSE)
gTree(children=do.call(gList,c(g2,list(g1))), cl = "custom_axis")
}
# gTrees don't know their size and ggplot would squash it, so give it room
grobHeight.custom_axis = heightDetails.custom_axis = function(x, ...)
unit(6, "lines")
ggplot(myd) +
geom_bar(aes(y = value, x = phase, fill = cat), stat="identity", position='dodge') +
theme_bw() +
theme(axis.text.x = my_axis(img),
axis.title.x = element_blank())
ggsave("test.png",p,width=10,height=8)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…