Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 向刻面ggplot geom_直方图添加多个文本注释_R_Ggplot2 - Fatal编程技术网

R 向刻面ggplot geom_直方图添加多个文本注释

R 向刻面ggplot geom_直方图添加多个文本注释,r,ggplot2,R,Ggplot2,我有以下数据帧: hist.df <- data.frame(y = c(rnorm(30,1,1), rnorm(15), rnorm(30,0,1)), gt = c(rep("ht", 30), rep("hm", 15), rep("hm", 30)), group = c(rep("sc", 30), rep("am", 15), rep("sc",30))) text.df = dat

我有以下
数据帧

hist.df <- data.frame(y = c(rnorm(30,1,1), rnorm(15), rnorm(30,0,1)), 
                      gt = c(rep("ht", 30), rep("hm", 15), rep("hm", 30)), 
                      group = c(rep("sc", 30), rep("am", 15), rep("sc",30)))
text.df = data.frame(ci.lo = c(0.001,0.005,-10.1), 
                     ci.hi = c(1.85,2.25,9.1), 
                     group = c("am","sc","sc"), 
                     factor = c("nu","nu","alpha"))

此外,我还有一个
数据框

hist.df <- data.frame(y = c(rnorm(30,1,1), rnorm(15), rnorm(30,0,1)), 
                      gt = c(rep("ht", 30), rep("hm", 15), rep("hm", 30)), 
                      group = c(rep("sc", 30), rep("am", 15), rep("sc",30)))
text.df = data.frame(ci.lo = c(0.001,0.005,-10.1), 
                     ci.hi = c(1.85,2.25,9.1), 
                     group = c("am","sc","sc"), 
                     factor = c("nu","nu","alpha"))
它定义了我要添加到刻面直方图中的文本注释,因此最终的图形将是:

因此
text.df$ci.lo
text.df$ci.hi
是相应
text.df$factor
的置信区间,它们通过text.df$group对应于刻面直方图

请注意,并非每个直方图都包含所有
text.df$factor

理想情况下,刻面直方图的ylim将为添加到直方图上方的文本留出足够的空间,以便它们仅出现在背景上


你知道如何做到这一点吗?

将我的评论总结成一个答案:

text.df$ci <- paste0(text.df$factor, ' = [', text.df$ci.lo, ', ', text.df$ci.hi, ']')
new_labels <- aggregate(text.df$ci, by = list(text.df$group), 
                        FUN = function(x) paste(x, collapse = '\n'))$x
hist.df$group <- factor(hist.df$group)
hist.df$group <- factor(hist.df$group,
                        labels = paste0(levels(hist.df$group), '\n', new_labels))

main.plot <- ggplot(data = hist.df, aes(x = y)) + 
  geom_histogram(alpha=0.5, position="identity", binwidth = 2.5, 
                 aes(fill = factor(gt))) + 
  facet_wrap(~group) + 
  scale_fill_manual(values = c("darkgreen","darkmagenta"), 
                    labels = c("ht","hm"), 
                    name = "gt")
main.plot + theme(strip.text = element_text(size=20))

<代码>文本.df$CI,您可以考虑将此注释添加到页眉中,例如“代码> Sc<代码>将沿着“代码>粘贴”(“SC”、“nu= [] ]、“alpha=[]”、“SEP=\n”)< /代码>。这将在各个方面保持一致,不会与直方图重叠。非常感谢。但我还是喜欢将其作为文本注释,因为它会使标题太大。