I am currently working in R, attempting to create a panel of plots that each contain two overlaying histograms: a red histogram underneath a blue histogram. The red histogram contains the same data set in each plot and thus should be displayed consistently across the board. I have found that this is not so. The red histogram differs, despite the data being exactly the same in each plot. Is there a way to fix this? Am I missing something in my code that is causing this inconsistency?
Here is the code I used to create the plots:
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, 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_250_p<-ggplot(data = test_4_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_100_p<-ggplot(data = test_5_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_50_p<-ggplot(data = test_6_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"))
full_plot<-plot_grid(draws_750_p, draws_500_p, draws_250_p, draws_100_p, draws_50_p, ncol = 3, nrow = 2)
And here is a picture of the odd results I am describing: Notice how the distribution of the red histogram differs despite the data set being exactly the same in each set (in this example you can see it the most in the draws_250_p plot in the right hand corner)-