I have created a histogram and have removed the outliers that are higher and lower the limits given by boxplot.stat
function. Then using cowplot
, I have added a boxplot using geom_boxplot
on top of the histogram. However, geom_boxplot
and boxplot.stat
give different values for all statistics hence the my boxplot shows outliers which have been removed in the histogram below. I tried to manually change the values of the boxplot by inserting the values from boxplot.stats
.
Here is a reproducible example:
set.seed(1)
Data <- data.table(
"X" = sample(1:100),
"Y" = sample(1:100))
)
numeric <-colnames(Data)
for (c in numeric){
#Remove Outliers:
stats <- boxplot.stats(Data[,get(c)])$stats
upper.lim <- stats[5]
median <- stats[3]
lower.lim <- stats[1]
df2<-Data[!(get(c)>upper.lim | get(c)<lower.lim),] #filter to remove outliers
plotmain <- ggplot(df2, aes(x=df2[,get(c)])) +
geom_histogram(colour="#102c59", fill="#5182d1", alpha=0.7) +
scale_x_continuous(labels = comma,breaks = scales::pretty_breaks(n = 10)) +
scale_y_continuous(labels = comma) +
ylab("Number of records") +
xlab(gsub("_", " ", c)) +
ggtitle(title) +
theme_grey(base_size=8)
xbox <- axis_canvas(plotmain, axis = "x", coord_flip = TRUE) +
geom_boxplot(data=df2,aes_string(x=c, ymin="lower.lim", lower=stats[2], middle="median", upper=stats[4], ymax="upper.lim"),stat="identity",
outlier.shape=NA) + coord_flip() #add boxplot on top of histogram
p1 <- insert_xaxis_grob(plotmain, xbox, grid::unit(0.3, "in"), position = "top")
histbox <- ggdraw(p1) #join the two plots
g<-grid.arrange(histbox, tbl, layout_matrix=rbind(c(1),c(1),c(1),c(1),c(2)), nrow=4)
histbox <- ggdraw(p1)
ggsave(file="g.png",width=20, height=15, units="cm")
}
The error I get is:
Error: geom_boxplot requires the following missing aesthetics: x
I have tried inserting x=1 and x="" in the aesthetic but that gives me an entirely filled black box.