统计表格 + 可视化 ,这个超强绘图技巧值得一看!!
如何將社會科學(social sciences) 中常用統計分析結果,如:簡單和交叉列表頻率(simple and cross tabulated frequencies)、直方圖(histograms)、箱線圖(box plots)、(廣義)線性模型((generalized) linear models)、混合效應模型(mixed effects models)、PCA和相關矩陣(PCA and correlation matrices)、聚類分析(cluster analyses) 、李克特量表(Likert scales) 等 有效的用可視化的方式表現出來?今天,小編就介紹一個非常優秀的可視化工具-R-sjPlot,接下來,就跟著小編一起來感受下這個包的魅力吧~~今天這篇推文的主要內容如下:
R-sjPlot包簡單介紹
R-sjPlot包實例演示
R-sjPlot包簡單介紹
R-sjPlot包作為一個優秀的R第三方包,其不僅可以繪制統計分析結果表格,而且還可以可視化統計模型結果,主要繪圖函數如下:
繪制回歸模型結果(部分):
plot_model()、plot_models()、plot_kfold_cv()
繪制描述性統計量(部分):
plot_frq():繪制變量的頻率。
plot_gpt():繪制分組比例表。
plot_likert():將李克特量表繪制為居中的堆疊條形圖。
plot_stackfrq():繪制堆積的比例條。
sjp.aov1():繪制單向方差分析表。
更多其他繪圖函數和參數,可參考:R-sjPlot包[1]
R-sjPlot包實例演示
這一部分小編選擇幾個常用的可視化結果進行展示,如下:
「樣例一」:plot_model()
library(tidyverse) library(ggtext) library(hrbrthemes) library(sjPlot) library(sjlabelled) library(sjmisc)#?構建數據 data(efc) y?<-?ifelse(efc$neg_c_7?<?median(na.omit(efc$neg_c_7)),?0,?1) #?create?data?frame?for?fitting?model df?<-?data.frame(y?=?to_factor(y),sex?=?to_factor(efc$c161sex),dep?=?to_factor(efc$e42dep),barthel?=?efc$barthtot,education?=?to_factor(efc$c172code) ) #?set?variable?label?for?response set_label(df$y)?<-?"High?Negative?Impact" #?fit?model m1?<-?glm(y?~.,?data?=?df,?family?=?binomial(link?=?"logit")) #可視化繪制 plot01?<-?plot_model(m1,vline.color?=?"red",show.values?=?TRUE,?value.offset?=?.3)?+?labs(title?=?"Example?of?<span style='color:#D20F26'>sjPlot::plot_model?function</span>",subtitle?=?"processed?charts?with?<span style='color:#1A73E8'>plot_model()</span>",caption?=?"Visualization?by?<span style='color:#0057FF'>DataCharm</span>")?+hrbrthemes::theme_ipsum(base_family?=?"Roboto?Condensed")?+theme(plot.title?=?element_markdown(hjust?=?0,vjust?=?.5,color?=?"black",size?=?20,?margin?=?margin(t?=?1,?b?=?12)),plot.subtitle?=?element_markdown(hjust?=?0,vjust?=?.5,size=15),plot.caption?=?element_markdown(hjust?=?1,face?=?'bold',size?=?12))Example01 Of plot_model Results當然,你還可以調整可視化結果外觀:
plot02?<-?plot_model(m1,?colors?=?"Accent",?show.values?=?TRUE,value.offset?=?.4,value.size?=?4,dot.size?=?3,line.size?=?1.5,vline.color?=?"#BC3C28",width?=?1.5 )?+ labs(title?=?"Example?of?<span style='color:#D20F26'>sjPlot::plot_model?function</span>",subtitle?=?"processed?charts?with?<span style='color:#1A73E8'>plot_model()</span>",caption?=?"Visualization?by?<span style='color:#0057FF'>DataCharm</span>")?+hrbrthemes::theme_ipsum(base_family?=?"Roboto?Condensed")?+theme(plot.title?=?element_markdown(hjust?=?0,vjust?=?.5,color?=?"black",size?=?20,?margin?=?margin(t?=?1,?b?=?12)),plot.subtitle?=?element_markdown(hjust?=?0,vjust?=?.5,size=15),plot.caption?=?element_markdown(hjust?=?1,face?=?'bold',size?=?12))Example02 Of plot_model Results「樣例二」:plot_kfold_cv()
plot03?<-?plot_kfold_cv(mtcars,?mpg?~.)?+labs(title?=?"Example?of?<span style='color:#D20F26'>sjPlot::plot_kfold_cv?function</span>",subtitle?=?"processed?charts?with?<span style='color:#1A73E8'>plot_kfold_cv()</span>",caption?=?"Visualization?by?<span style='color:#0057FF'>DataCharm</span>")?+hrbrthemes::theme_ipsum(base_family?=?"Roboto?Condensed")?+theme(plot.title?=?element_markdown(hjust?=?0.5,vjust?=?.5,color?=?"black",size?=?20,?margin?=?margin(t?=?1,?b?=?12)),plot.subtitle?=?element_markdown(hjust?=?0,vjust?=?.5,size=15),plot.caption?=?element_markdown(hjust?=?1,face?=?'bold',size?=?12))Example01 Of plot_kfold_cv()「樣例三」:plot_frq()
data(iris) ageGrp?<-?group_var(efc$e17age) ageGrpLab?<-?group_labels(efc$e17age) plot04?<-?plot_frq(geom.colors?=?"#BC3C28",ageGrp,axis.labels?=?ageGrpLab)?+labs(title?=?"Example?of?<span style='color:#D20F26'>sjPlot::plot_frq?function</span>",subtitle?=?"processed?charts?with?<span style='color:#1A73E8'>plot_frq()</span>",caption?=?"Visualization?by?<span style='color:#0057FF'>DataCharm</span>")?+hrbrthemes::theme_ipsum(base_family?=?"Roboto?Condensed")?+theme(plot.title?=?element_markdown(hjust?=?0.5,vjust?=?.5,color?=?"black",size?=?20,?margin?=?margin(t?=?1,?b?=?12)),plot.subtitle?=?element_markdown(hjust?=?0,vjust?=?.5,size=15),plot.caption?=?element_markdown(hjust?=?1,face?=?'bold',size?=?12))Example Of plot_frq()你還可以繪制這樣的可視化效果:
plot05?<-?plot_frq(efc$c160age,?type?=?"h",?show.mean?=?TRUE,?geom.colors?=?"#0072B5",show.mean.val?=?TRUE,normal.curve?=?TRUE,?show.sd?=?TRUE,?normal.curve.color?=?"#BC3C28",normal.curve.size?=?3,?ylim?=?c(0,50))?+labs(title?=?"Example?of?<span style='color:#D20F26'>sjPlot::plot_frq?function</span>",subtitle?=?"processed?charts?with?<span style='color:#1A73E8'>plot_frq()</span>",caption?=?"Visualization?by?<span style='color:#0057FF'>DataCharm</span>")?+hrbrthemes::theme_ipsum(base_family?=?"Roboto?Condensed")?+theme(plot.title?=?element_markdown(hjust?=?0.5,vjust?=?.5,color?=?"black",size?=?20,?margin?=?margin(t?=?1,?b?=?12)),plot.subtitle?=?element_markdown(hjust?=?0,vjust?=?.5,size=15),plot.caption?=?element_markdown(hjust?=?1,face?=?'bold',size?=?12))Example02 Of plot_frq()「樣例四」:plot_likert()
mydf?<-?find_var(efc,?pattern?=?"cop",?out?=?"df") plot06?<-?plot_likert(mydf,geom.colors?=?"RdBu",grid.range?=?c(1.2,?1.4),expand.grid?=?FALSE,values?=?"sum.outside",show.prc.sign?=?TRUE)?+labs(title?=?"Example?of?<span style='color:#D20F26'>sjPlot::plot_likert?function</span>",subtitle?=?"processed?charts?with?<span style='color:#1A73E8'>plot_likert()</span>",caption?=?"Visualization?by?<span style='color:#0057FF'>DataCharm</span>")?+hrbrthemes::theme_ipsum(base_family?=?"Roboto?Condensed")?+theme(plot.title?=?element_markdown(hjust?=?0.5,vjust?=?.5,color?=?"black",size?=?20,?margin?=?margin(t?=?1,?b?=?12)),plot.subtitle?=?element_markdown(hjust?=?0,vjust?=?.5,size=15),plot.caption?=?element_markdown(hjust?=?1,face?=?'bold',size?=?12))Example Of plot_likert()「樣例五」:tab_model()繪制表格
data("efc") efc?<-?as_factor(efc,?c161sex,?c172code) m1?<-?lm(barthtot?~?c160age?+?c12hour?+?c161sex?+?c172code,?data?=?efc) m2?<-?lm(neg_c_7?~?c160age?+?c12hour?+?c161sex?+?e17age,?data?=?efc) tab_model(m1,?m2,?p.style?=?"scientific",?digits.p?=?2)Example Of tab_model()以上就是小編簡單的介紹了R-sjPlot包的簡單例子,希望感興趣的小伙伴可以看些,更多詳細案例和函數參數大家可閱讀官網(參考資料[1])即可。
總結
今天小編給大家分享的這個寶藏可視化工具R-sjPlot包 ,不僅可以繪制出定制化的可視化作品,而且對一些統計表格更是繪制出自己喜歡的風格,希望可以給大家以后繪圖提供不一樣的繪圖選擇。
參考資料
[1]
R-sjPlot包: https://strengejacke.github.io/sjPlot/reference/index.html。
各位伙伴們好,詹帥本帥搭建了一個個人博客和小程序,匯集各種干貨和資源,也方便大家閱讀,感興趣的小伙伴請移步小程序體驗一下哦!(歡迎提建議)推薦閱讀牛逼!Python常用數據類型的基本操作(長文系列第①篇) 牛逼!Python的判斷、循環和各種表達式(長文系列第②篇)牛逼!Python函數和文件操作(長文系列第③篇)牛逼!Python錯誤、異常和模塊(長文系列第④篇)總結
以上是生活随笔為你收集整理的统计表格 + 可视化 ,这个超强绘图技巧值得一看!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超萌哒!20000个虎年限定红包封面,限
- 下一篇: 粉丝给我发色情app,我反手对色情app