group by 保留哪一条数据_使用R语言绘制一维数据统计图总结
生活随笔
收集整理的這篇文章主要介紹了
group by 保留哪一条数据_使用R语言绘制一维数据统计图总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
加載數據 繪制莖葉圖 繪制直方圖 繪制概率密度曲線 繪制小提琴圖 繪制箱線圖 繪制小提琴圖箱線圖 集中趨勢統計 分散程度 apply的使用
加載數據
模擬數據下載
library(tidyverse) cjb <- read.csv("/home/wy/Downloads/cjb.csv",header = TRUE,stringsAsFactors = FALSE,fileEncoding = "UTF-8")繪制莖葉圖
cjb %>% filter(bj == '1101') %>%select(sx) %>%as_vector() %>%stem() 5 | 57996 | 00146 | 557897 | 0000111223344447 | 7888998 | 1112223344448 | 5899 | 224繪制直方圖
sx_hist_result = hist(cjb$sx,plot = FALSE) typeof(sx_hist_result) names(sx_hist_result) # 使用ggplot繪制與hist相同的直方圖 ggplot(data = cjb,mapping = aes(sx))+geom_histogram(breaks = sx_hist_result$breaks,color = "darkgray",fill = "white")+stat_bin(breaks = sx_hist_result$breaks,geom = "text",aes(label = ..count..))+coord_flip()繪制概率密度曲線
ggplot(data = cjb,mapping = aes(sx))+geom_histogram(breaks = sx_hist_result$breaks,color = "darkgray",fill = "white",aes(y = ..density.. ))+geom_density(color = 'blue')繪制小提琴圖
ggplot(cjb,aes(x=factor(0),y=sx))+geom_violin(fill="orange",alpha=0.2)+coord_flip()繪制箱線圖
cjb %>%ggplot(aes(x=factor(0),y=sx))+geom_boxplot(width=0.25,fill = "#E69F00",outlier.colour = "red",outlier.shape = 1,outlier.size = .2)+geom_rug(position = "jitter",size=0.1,sides = "l")+coord_flip()boxplot.stats(cjb$sx) # $stats 下邊界 一分位距 中位數 三分位距 上邊界 # [1] 60 81 89 95 100 # $n 數據記錄數 # [1] 775 # $conf # [1] 88.20543 89.79457 # $out 異常點 # [1] 55 59 57 59 58 51 56 55 59 26 58 46 0 59 59繪制小提琴圖+箱線圖
cjb %>%ggplot(aes(x=factor(0),y=sx)) +geom_violin(fill="#56B4E9",width=0.75) +geom_boxplot(width=0.25,fill = "#E69F00",outlier.colour = "red",outlier.shape = 1,outlier.size = 2)+geom_rug(position = "jitter",size=0.1,sides = "l")+coord_flip()集中趨勢統計
cjb %>%group_by(wlfk) %>% # 按文理分科分組統計summarise(count = n(), # 各組人數sx_median = median(sx), # 中位數sx_mean = mean(sx) # 均值) # A tibble: 2 x 4 # wlfk count sx_median sx_mean # <chr> <int> <dbl> <dbl> # 1 文科 394 84 82.7 # 2 理科 381 93 89.5分散程度
cjb %>%group_by(wlfk) %>% # 按文理分科分組統計summarise(sx_max = max(sx), # 最大值sx_min = min(sx), # 最小值sx_range = max(sx) - min(sx) # 極差)# A tibble: 2 x 4 # wlfk sx_max sx_min sx_range # <chr> <int> <int> <int> # 1 文科 100 26 74 # 2 理科 100 0 100 cjb %>%group_by(wlfk) %>% # 按文理分科分組統計summarise(sx_O3 = quantile(sx,3/4), # 第三分位數sx_min = quantile(sx,1/4), # 第一分位數sx_iqr = IQR(sx) # 四分位距) # A tibble: 2 x 4 # wlfk sx_O3 sx_min sx_iqr # <chr> <dbl> <dbl> <dbl> # 1 文科 92 75 17 # 2 理科 96 86 10apply的使用
round(apply(cjb[,4:12], 2, function(x){c(mean = mean(x),median = median(x),range = diff(range(x)),IQR = IQR(x)) })) # yw sx wy zz ls dl wl hx sw # mean 87 86 87 92 89 93 81 92 86 # median 88 89 88 93 90 94 83 94 88 # range 96 100 99 100 100 100 100 100 100 # IQR 6 14 8 5 10 6 17 10 12R語言?www.bioinfo.online總結
以上是生活随笔為你收集整理的group by 保留哪一条数据_使用R语言绘制一维数据统计图总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java参考文献英文_java论文英文的
- 下一篇: 游戏 Npk文件解压