大佬教程收集整理的这篇文章主要介绍了R geom_histogram position="identity" 不一致,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前在 R 中工作,尝试创建一组图,每个图都包含两个重叠的直方图:蓝色直方图下方的红色直方图。红色直方图在每个图中都包含相同的数据集,因此应全面一致地显示。我发现事实并非如此。尽管每个图中的数据完全相同,但红色直方图有所不同。有没有办法来解决这个问题?我的代码中是否遗漏了导致这种不一致的内容?
这是我用来创建绘图的代码:
@H_801_7@ test<-rnorm(1000) test<-as.data.table(test) test[,type:="Sample"] setnames(test,old="test",new="value") test_2<-rnorm(750) test_2<-as.data.table(test_2) test_2[,type:="Sub Sample"] setnames(test_2,old="test_2",new="value") test_2_final<-rbind(test,test_2,fill=TRUE) test_3<-rnorm(500) test_3<-as.data.table(test_3) test_3[,type:="Sub Sample"] setnames(test_3,old="test_3",new="value") test_3_final<-rbind(test,test_3,fill=TRUE) test_4<-rnorm(250) test_4<-as.data.table(test_4) test_4[,type:="Sub Sample"] setnames(test_4,old="test_4",new="value") test_4_final<-rbind(test,test_4,fill=TRUE) test_5<-rnorm(100) test_5<-as.data.table(test_5) test_5[,type:="Sub Sample"] setnames(test_5,old="test_5",new="value") test_5_final<-rbind(test,test_5,fill=TRUE) test_6<-rnorm(50) test_6<-as.data.table(test_6) test_6[,type:="Sub Sample"] setnames(test_6,old="test_6",new="value") test_6_final<-rbind(test,test_6,fill=TRUE) draws_750_p<-ggplot(data = test_2_final,aes(x=value,fill=type,color=typE)) + geom_histogram(position="IDentity",Alpha = 0.2,bins=30) + theme(plot.title = element_text(hjust = 0.5,size=10,face="plain")) draws_500_p<-ggplot(data = test_3_final,face="plain")) draws_250_p<-ggplot(data = test_4_final,face="plain")) draws_100_p<-ggplot(data = test_5_final,face="plain")) draws_50_p<-ggplot(data = test_6_final,face="plain")) full_plot<-plot_grID(draws_750_p,draws_500_p,draws_250_p,draws_100_p,draws_50_p,ncol = 3,nrow = 2)
这是我描述的奇怪结果的图片:注意红色直方图的分布如何不同,尽管每个组中的数据集完全相同(在本例中,您可以在 draws_250_p 图中看到最多)在右上角)-
正如我在评论中提到的,问题是每个图使用的垃圾箱都不同。这意味着相同的值可能会出现在不同的 bin 中。默认值是根据指定的 bin 数量和数据范围猜测合理的 bin 边界,但由于每个图中的子样本不同(并且可能比主样本更早或晚开始),结果边界将是不一样。
解决方案是直接指定 bin 边界,以便它们在每个图中都相同。下面是使用 @H_801_7@binwidth 和 @H_801_7@boundary 的组合隐式指定 bin 边界的示例。我还冒昧地将所有值组合到一个数据框中,以便可以使用 @H_801_7@faCET_wrap 一次绘制它们,其优点是对齐各个面的轴并用大小标记它们子样本。不过,关键点在于对 @H_801_7@geom_histogram 的调用。您现在可以看到每个方面的红色分布都相同。
@H_801_7@library(tidyversE) test <- tibble(type = "Sample",value = rnorm(1000)) add_sub_sample <- function(n,df) { sub_sample <- tibble(type = "Sub Sample",value = rnorm(n)) df %>% rbind(sub_samplE) %>% mutate(sub_sample_n = n) } test_final <- c(750,500,250,100,50) %>% map(add_sub_sample,test) %>% bind_rows() ggplot(test_final,aes(x = value,fill = type,colour = typE)) + geom_histogram(position = "identity",alpha = 0.2,binwidth = 0.2,boundary = 0) + faCET_wrap(~sub_sample_n) + theme(plot.title = element_text(hjust = 0.5,size=10,face="plain"))
由 reprex package (v1.0.0) 于 2021 年 7 月 14 日创建
以上是大佬教程为你收集整理的R geom_histogram position="identity" 不一致全部内容,希望文章能够帮你解决R geom_histogram position="identity" 不一致所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。