[英]ggplot2 position annotation_custom outside for multiple graphs on one page
I have a plot doing bar plot for categorical variable (eg Group A, Group B, ...). 我有一个图,对类别变量(例如A组,B组,...)进行条形图绘制。 I want to have multiple graphs on one page (something like this ).
我想在一页上有多个图(像这样 )。 I want to create a text outside the plot using:
我想使用以下方法在图外创建文本:
###### Plot three graphs together with one legend and one text on the right side
## this is the function that make one legend for 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 three graphs with legend
p <- grid.arrange(arrangeGrob(p1 , p2 , p3, mylegend, ncol = 4, widths = c(20, 20, 20, 11))) ## vertical
### create text to put outside this 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 an error "Error in non-numeric argument to binary operator". 它给我一个错误“二进制运算符的非数字参数错误”。 Since x axis is categorical variable, so I thought the error was caused by xmax, xmin values cannot be assigned to categorical data.
由于x轴是分类变量,因此我认为该错误是由xmax引起的,因此无法将xmin值分配给分类数据。 So I used
p1 = p + annotation_custom(grob = Text1,xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
. 所以我用了
p1 = p + annotation_custom(grob = Text1,xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
。 Again this same error. 同样是同样的错误。
I know how to do legend outside for multi-graph plot based on this reference: ggplot2: multiple plots with different variables in a single row, single grouping legend 我知道如何基于此参考为多图绘图做图例: ggplot2:在单行中具有不同变量的多个图,单个分组图例
The text outside example of Displaying text below the plot generated by ggplot2 is helpful. 在ggplot2生成的绘图下方显示文本示例之外的文本很有帮助。 But this is for one graph, and I don't find it useful in my multi-graph plot situation.
但这是针对一张图的,在我使用多图的情况下,我认为它没有用。
Any idea about the reason of errors or how to position the annotation for multiple graphs one page with categorical x/y variables? 关于错误原因或如何将多个图形的注释放置在具有分类x / y变量的页面上的任何想法?
Something like this, where you have a factor variable on the x axis and use the factor integers to position the text grob? 像这样,在x轴上有一个因子变量,并使用因子整数来放置文本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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.