i need replace 1 facet wrap label complicated expression. however, cannot function facet_wrap_labeller work anymore plots created under older ggplot2 versions.
data <- as.data.frame(matrix(rnorm(60),20,3)) data$factor <- c(rep("f1", 4),rep("f2", 4), rep("f3", 4), rep("f4",4),rep("f5", 4)) names(data) <- c("a", "b", "c", "factor") melted <- melt(data) p <- ggplot(melted, aes(variable, value)) + stat_boxplot(geom ='errorbar') + geom_boxplot() p1 <- p + facet_wrap(~factor) facet_wrap_labeller(p1, labels=c("a", "b", expression({}^2*italic(d)~textstyle(group("(", rarefied, ")")))))
and get:
error in getgrob(gg[[strips[ii]]], "strip.text", grep = true, global = true) : valid child "gtree" called from: getgrob(gg[[strips[ii]]], "strip.text", grep = true, global = true)
i believe happening because of ggplot2 2.0 not being compatible function.
i tried labeller=labeller()
, , labeller=bquote()
arguments, when tried use vector
newlabels <- c(a="a", b="b",, d=expression({}^0*italic(d)~textstyle(group("(", rarefied, ")"))))
in
p1 <- p + facet_wrap(~factor, labeller = labeller(factor=newlabels))
the expression ignored , original factor levels plotted. bquote, dont know add more 1 name argument.
any appreciated.
there appear 4 labels 5 facets in example, use labeller=label_parsed
, rename factor levels easily, avoiding need function.
## rename levels (i used data.table::melt, characters) melted$factor <- factor(melted$factor, levels=paste0('f', 1:5), labels=c('a', 'b', 'c', '{}^0*italic(d)~plain((rarefied))', 'e')) p <- ggplot(melted, aes(variable, value)) + stat_boxplot(geom ='errorbar') + geom_boxplot() p + facet_wrap(~factor, labeller=label_parsed)
Comments
Post a Comment