赞
踩
如何将社会科学(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第三方包,其不仅可以绘制统计分析结果表格,而且还可以可视化统计模型结果,主要绘图函数如下:
绘制回归模型结果(部分):
plot_model()、plot_models()、plot_kfold_cv()
绘制描述性统计量(部分):
plot_frq():绘制变量的频率。
plot_gpt():绘制分组比例表。
plot_likert():将李克特量表绘制为居中的堆叠条形图。
plot_stackfrq():绘制堆积的比例条。
sjp.aov1():绘制单向方差分析表。
更多其他绘图函数和参数,可参考:R-sjPlot包[1]
这一部分小编选择几个常用的可视化结果进行展示,如下:
「样例一」: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))
当然,你还可以调整可视化结果外观:
- 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))
「样例二」: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))
「样例三」: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))
你还可以绘制这样的可视化效果:
- 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))
「样例四」: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))
「样例五」: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)
以上就是小编简单的介绍了R-sjPlot包的简单例子,希望感兴趣的小伙伴可以看些,更多详细案例和函数参数大家可阅读官网(参考资料[1])即可。
今天小编给大家分享的这个宝藏可视化工具R-sjPlot包 ,不仅可以绘制出定制化的可视化作品,而且对一些统计表格更是绘制出自己喜欢的风格,希望可以给大家以后绘图提供不一样的绘图选择。
[1]
R-sjPlot包: https://strengejacke.github.io/sjPlot/reference/index.html。
各位伙伴们好,詹帅本帅搭建了一个个人博客和小程序,汇集各种干货和资源,也方便大家阅读,感兴趣的小伙伴请移步小程序体验一下哦!(欢迎提建议)
- 推荐阅读
-
-
-
- 牛逼!Python常用数据类型的基本操作(长文系列第①篇)
- 牛逼!Python的判断、循环和各种表达式(长文系列第②篇)
-
- 牛逼!Python函数和文件操作(长文系列第③篇)
-
- 牛逼!Python错误、异常和模块(长文系列第④篇)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。