Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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 ggplot2-手动确定“所需的组数”;填写「;不完整数据集中aes映射中的变量_R_Ggplot2_Mapping_Facet Wrap - Fatal编程技术网

R ggplot2-手动确定“所需的组数”;填写「;不完整数据集中aes映射中的变量

R ggplot2-手动确定“所需的组数”;填写「;不完整数据集中aes映射中的变量,r,ggplot2,mapping,facet-wrap,R,Ggplot2,Mapping,Facet Wrap,我想生成分面盒形图,并将其与多个实验室参数的geom_点叠加。曲线图应沿年龄组分割,在各年龄组中,应相邻比较患者的状态。 以下是我的映射的外观: aes(x=年龄组2,y=值,填充=状态) 以下是生成我的报告的总体代码: pdf("lab10.pdf") for (i in 1:number_of_facets_lab){ print(ggplot(lab_merge_clean, aes(x=Age_group2, y=value, fill=Status)) +

我想生成分面盒形图,并将其与多个实验室参数的geom_点叠加。曲线图应沿年龄组分割,在各年龄组中,应相邻比较患者的状态。 以下是我的映射的外观: aes(x=年龄组2,y=值,填充=状态)

以下是生成我的报告的总体代码:

pdf("lab10.pdf")
for (i in 1:number_of_facets_lab){
  print(ggplot(lab_merge_clean, aes(x=Age_group2, y=value, fill=Status)) +
          geom_boxplot(outlier.shape = NA, position = position_dodge2(width = 0.7,preserve="total"), 
          width = 0.6) +
          geom_point(data = lab_merge_clean %>% filter(max_WHO_classification >= 3 & 
          analysis.identifier != "COV006"),
                     position = position_dodge(width = 0.6, preserve="total"),
                     shape = 16,alpha = 0.7, size = 2.5)+
          geom_point(data = lab_merge_clean %>% filter(max_WHO_classification < 3),
                     position = position_dodge(width = 0.6, preserve="total"),
                     shape = 1, alpha = 0.7, size = 2.5) +
          geom_point(data = lab_merge_clean %>% filter(analysis.identifier=="COV006" ),
                     position = position_dodge(width = 0.6, preserve = "total"),
                     shape = 1, alpha = 0.7, size = 2.5, colour = "red", fill = "red") +
          scale_fill_manual(values = c("lightskyblue2", "orange2"))+
          facet_wrap_paginate(~ lab_parameter, scales = "free_y", ncol = 1, nrow = 1, page = i) +
          theme_bw() +
          stat_compare_means(aes(label = paste0("p =", ..p.format..))) +
          scale_y_continuous(expand = expansion(mult = c(0.05, 0.2))) +theme_classic() + 
          theme( axis.text = element_text( size = 14 ),
                 axis.text.x = element_text( size = 20 ),
                 axis.title = element_text( size = 16, face = "bold" ),
                 legend.position="none",
                 strip.text = element_text(size = 20))
        )
  print(i)
}
dev.off()
pdf(“lab10.pdf”)
用于(1中的i:面数实验室){
打印(ggplot(实验室合并清洁,aes(x=年龄组2,y=值,填充=状态))+
geom_箱线图(outlier.shape=NA,position=position_-dodge2(width=0.7,preserve=“total”),
宽度=0.6)+
几何点(数据=实验室合并清洁%>%过滤器(最大分类>=3和
analysis.identifier!=“COV006”),
位置=位置减淡(宽度=0.6,preserve=“total”),
形状=16,α=0.7,尺寸=2.5)+
几何点(数据=实验室合并清洁%>%过滤器(最大分类<3),
位置=位置减淡(宽度=0.6,preserve=“total”),
形状=1,α=0.7,尺寸=2.5)+
geom_point(数据=lab_merge_clean%>%过滤器(analysis.identifier==“COV006”),
位置=位置减淡(宽度=0.6,preserve=“total”),
形状=1,α=0.7,尺寸=2.5,颜色=“红色”,填充=“红色”)+
刻度填充手动(数值=c(“lightskyblue2”、“橙色2”))+
刻面包裹分页(~lab_参数,scales=“free_y”,ncol=1,nrow=1,page=i)+
主题_bw()+
统计比较意味着(aes(标签=粘贴0(“p=”,…p.format…))+
连续缩放(扩展=扩展(mult=c(0.05,0.2))+theme_classic()+
主题(axis.text=元素\文本(大小=14),
axis.text.x=元素\文本(大小=20),
axis.title=元素\文本(大小=16,面=“粗体”),
legend.position=“无”,
strip.text=元素\文本(大小=20))
)
印刷品(一)
}
发展主任()
在最佳情况下,结果如下所示:

但有时会发生这种情况:

箱线图移动,因为在另一组中没有观察到。这就是为什么我想手动固定fill变量中的组数,这样绘图就不会移位并与geom_点对齐


我已经在堆栈上查找了解决方案,但还没有找到答案。任何帮助都将不胜感激

您是否正在寻找
位置2
preserve=“single”
但在
geom\u点上有
preserve=“total”

库(ggplot2)
种子(1)
df