R語(yǔ)言:頻數(shù)柱形圖、直方圖,數(shù)據(jù)歸一化,標(biāo)準(zhǔn)化,取平方根,log10處理

導(dǎo)讀

統(tǒng)計(jì)一組數(shù)據(jù)的頻數(shù)或給改組數(shù)據(jù)分類,然后分別用ggplot geom_bar、geom_histogram畫(huà)柱形圖和直方圖。接著,對(duì)數(shù)據(jù)分別進(jìn)行歸一化,標(biāo)準(zhǔn)化,取平方根,log10處理,并繪制直方圖。直方圖顯示平方根和log10處理可以將偏態(tài)數(shù)據(jù)轉(zhuǎn)成僅正太分布(可以做正態(tài)性檢驗(yàn))。

一、數(shù)據(jù)產(chǎn)生

# 模擬數(shù)據(jù)
data = c(sample(c(1:100), 80, replace=F),
         sample(c(100:200), 20, replace=F),
         sample(c(200:300), 10, replace=F))

二、計(jì)算頻數(shù)和畫(huà)柱形圖

1 計(jì)算頻數(shù)

# 定義頻數(shù)表,計(jì)數(shù)
df = data.frame(label=seq(30, 300, by=30), count=rep(0, 10))
for(i in data)
{
    if(i < 30){df[1, 2]=df[1, 2]+1}
    else if(i < 60){df[2, 2]=df[2, 2]+1}
        else if(i < 90){df[3, 2]=df[3, 2]+1}
            else if(i < 120){df[4, 2]=df[4, 2]+1}
                else if(i < 150){df[5, 2]=df[5, 2]+1}
                    else if(i < 180){df[6, 2]=df[6, 2]+1}   
                        else if(i < 210){df[7, 2]=df[7, 2]+1}
                            else if(i < 240){df[8, 2]=df[8, 2]+1}
                                else if(i < 270){df[9, 2]=df[9, 2]+1}
                                    else if(i < 300 | i == 300){df[10, 2]=df[10, 2]+1}
}

2 柱形圖

# 條形圖                          
ggplot(df, aes(x=label, y=count, fill=as.character(rownames(df)))) +
  geom_bar(stat="identity") +
  theme_classic() +
  theme(legend.position="")

三、添加分組和畫(huà)直方圖

1 添加分組

label = c()
for(i in data)
{
    if(i < 30){label = c(label, 30)}
    else if(i < 60){label = c(label, 60)}
        else if(i < 90){label = c(label, 90)}
            else if(i < 120){label = c(label, 120)}
                else if(i < 150){label = c(label, 150)}
                    else if(i < 180){label = c(label, 180)}   
                        else if(i < 210){label = c(label, 210)}
                            else if(i < 240){label = c(label, 240)}
                                else if(i < 270){label = c(label, 270)}
                                    else if(i < 300 | i == 300){label = c(label, 300)}
}

df2 = data.frame(label=label, num=data)
df2

2 畫(huà)直方圖

ggplot(df2, aes(x=label, fill=as.factor(label))) + 
  geom_histogram(binwidth=30, color="black") +
  theme_classic() +
  theme(legend.position="")

四、上面愚蠢了,geom_histogram不需要分組和計(jì)數(shù)

直接用原始數(shù)據(jù)直方圖,也就是df2第二列

ggplot(df2, aes(x=num)) + 
  geom_histogram(binwidth=30, fill="lightblue", color="black") +
  theme_classic()

修改參數(shù)binwidth

ggplot(df2, aes(x=num)) + 
  geom_histogram(fill="lightblue", color="black") +
  theme_classic()

五、歸一化后的直方圖

1 歸一化

df2$to_one = df2$num/sum(df2$num)

2 直方圖

ggplot(df2, aes(x=to_one)) + 
  geom_histogram(fill="lightblue", color="black") +
  theme_classic()

完全同上

六、標(biāo)準(zhǔn)化后的直方圖

1 數(shù)據(jù)標(biāo)準(zhǔn)化

average = mean(df2$num)
standard = sd(df2$num) 
df2$norm = (df2$num - average)/standard

2 直方圖

ggplot(df2, aes(x=norm)) + 
  geom_histogram(fill="lightblue", color="black") +
  theme_classic()

沒(méi)什么好轉(zhuǎn)

七、平方根后直方圖

1 計(jì)算平方根

df2$squart = sqrt(df2$num)

2 直方圖

ggplot(df2, aes(x=squart)) + 
  geom_histogram(fill="lightblue", color="black") +
  theme_classic()

有效果了

八、log10后直方圖

1 計(jì)算log10

df2$log10 = log10(df2$num)

2 直方圖

ggplot(df2, aes(x=log10)) + 
  geom_histogram(fill="lightblue", color="black") +
  theme_classic()

也有效果

參考:
R語(yǔ)言-數(shù)據(jù)轉(zhuǎn)化(log和根號(hào))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容