R中的直方图频率

时间:2021-05-17 11:57:21

标签: r

这是我的原始图表。

bins = seq(0,20,by=1)
freq = cut(A$`Type`,bins)
table(freq)
transform(table(freq))
par(mar=c(1,1,1,1))
hist(A$`Type`,prob = TRUE,xlim= c(0,20),ylim= c(0,0.4),breaks=20,main="Frequency Distribution",xlab="Frequency",col="lightblue")

bins = seq(0,20,by=1)
freq = cut(B$`Type`,bins)
table(freq)
transform(table(freq))
par(mar=c(1,1,1,1))
hist(B$`Type`,prob = TRUE,xlim= c(0,20),ylim= c(0,0.4),breaks=20,main="Frequency Distribution",xlab="Frequency",col="lightblue")

我的细化(想结合图) 基本原理:计数 A(类型:1 2 3 4 5 ...更改为因子,并计算 A 在类型 1 类型 2 中的频率)和 B(类型:1 2 3 4 5 ...更改为因子,并计数B 在 Type 1 Type 2 中的频率 )

desired: 
Person Type Frequency
A       1      3
B       1      4
A       2      0
B       2      7
...

hist1=rbind.data.frame(A,B)
hist1
structure(list(`Type` = c(12, 11, 10, 9, 7, 13, 9, 7, 6, 16, 11, 9), `Person`=c(
"A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B")

p<-ggplot(hist1, aes(x=`Type`))+
  geom_histogram(aes(y = stat(count) / sum(count)), bins = 30)+
  geom_histogram(color="black", fill="white")+
  facet_grid(Person~ .)+
  scale_y_continuous(labels = scales::percent)
p

即。在直方图中 enter image description here

0 个答案:

没有答案