[英]How to add summary statistics in histogram plot using ggplot2?
我想在使用ggplot2
制作的直方圖中添加匯總統計信息。 我正在使用以下代碼
#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)
每個方面都有所有變量的匯總統計。 我希望它應該只顯示分面變量的匯總統計信息。 怎么做?
您需要拆分 data.frame:
p1+geom_table_npc(data=summ,label =split(summ,summ$variable),
npcx = 0.00, npcy = 1, hjust = 0, vjust = 1,size=2)
或嵌套您擁有的匯總表:
summ <- summ %>% nest(data=-c(variable))
# A tibble: 4 x 2
variable data
<fct> <list<df[,9]>>
1 Sepal.Length [1 × 9]
2 Sepal.Width [1 × 9]
3 Petal.Length [1 × 9]
4 Petal.Width [1 × 9]
p1+geom_table_npc(data = summ,label =summ$data,
,npcx = 0.00, npcy = 1, hjust = 0, vjust = 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.