I want to add summary statistics in histogram plot made using ggplot2
. I am using the following code
#Loading the required packages
library(dplyr)
library(ggplot2)
library(reshape2)
library(moments)
library(ggpmisc)
#Loading the data
df <- iris
df.m <- melt(df, id="Species")
#Calculating the summary statistics
summ <- df.m %>%
group_by(variable) %>%
summarize(min = min(value), max = max(value),
mean = mean(value), q1= quantile(value, probs = 0.25),
median = median(value), q3= quantile(value, probs = 0.75),
sd = sd(value), skewness=skewness(value), kurtosis=kurtosis(value))
#Histogram plotting
p1 <- ggplot(df.m) + geom_histogram(aes(x = value), fill = "grey", color = "black") +
facet_wrap(~variable, scales="free", ncol = 2)+ theme_bw()
p1+geom_table_npc(data = summ, label = list(summ),npcx = 0.00, npcy = 1, hjust = 0, vjust = 1)
It is giving me the following plot
Every facet is having summary statistics of all the variables. I want it should show the summary statistics of the faceted variable only. How to do it?