r - ggplot2 position annotation_custom outside for multiple graphs on one page -
i have plot doing bar plot categorical variable (e.g. group a, group b, ...). want have multiple graphs on 1 page (something this). want create text outside plot using:
###### plot 3 graphs 1 legend , 1 text on right side ## function make 1 legend multiple graphs g_legend <- function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend <- tmp$grobs[[leg]] return(legend)} mylegend <- g_legend(p3.legend) ### combine 3 graphs legend p <- grid.arrange(arrangegrob(p1 , p2 , p3, mylegend, ncol = 4, widths = c(20, 20, 20, 11))) ## vertical ### create text put outside multi-graph plot text1 = textgrob(paste("an example text")) p1 = p + annotation_custom(grob = text1, xmin = 1, xmax = 2, ymin = 20, ymax = 30)
it gave me error "error in non-numeric argument binary operator". since x axis categorical variable, thought error caused xmax, xmin values cannot assigned categorical data. used p1 = p + annotation_custom(grob = text1,xmin = -inf, xmax = inf, ymin = -inf, ymax = inf)
. again same error.
i know how legend outside multi-graph plot based on reference: ggplot2: multiple plots different variables in single row, single grouping legend
the text outside example of displaying text below plot generated ggplot2 helpful. 1 graph, , don't find useful in multi-graph plot situation.
any idea reason of errors or how position annotation multiple graphs 1 page categorical x/y variables?
something this, have factor variable on x axis , use factor integers position text grob?
require(ggplot2) df <- data.frame(a = seq(0, 30, 6), group = as.factor(c("a", "b", "c", "d", "e", "f"))) text1 <- textgrob("test text") ggplot(data = df, aes(x = group, y = a)) + geom_bar(stat = "identity") + annotation_custom(grob = text1, xmin = 2, xmax = 3, ymin = 20, ymax = 22)
Comments
Post a Comment