还有这样的拟合操作!?这个可视化工具太牛了~~
今天小编给大家介绍一下非常优秀的可视化技巧-R-ggalt包绘图技巧,该包的主要特色是提供geom_xspline()、geom_bkde()、geom_encircle()、geom_lollipop()和geom_dumbbell() 等优秀绘图函数,接下来,小编详细介绍这这几个绘图函数,内容如下:
geom_xspline()
该绘图函数主要用于使用X样条连接控制点的样式,这里和ggplot2的geom_smooth()函数进行比较,首先看一下原始的可视化样式:
- geom_line()+geom_point()
# 构建数据
set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10),
y=c(sample(15:30, 10), 2*sample(15:30, 10), 3*sample(15:30, 10)),
group=factor(c(rep(1, 10), rep(2, 10), rep(3, 10))))
ggplot(dat, aes(x, y, group=group, fill=group,color=group)) +
geom_line(size=1) +
geom_point(shape=21,size=5,colour="black") +
ggsci::scale_fill_aaas() +
ggsci::scale_color_aaas()+
labs(
title = "Example of <span style='color:#D20F26'>ggplot2::geom_point() and geom_line() function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_point() and geom_line()</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(face = 'bold',size = 12))
Example Of geom_line()+geom_point()
- geom_smooth()
添加拟合函数geom_smooth()的结果:
+
geom_line(size=1) +
geom_point(shape=21,size=5,colour="black") +
geom_smooth(se=FALSE, linetype="dashed", size=1)
+
Example Of geom_line()+geom_point()+geom_smooth
- geom_xspline()
这里便于区分,使用虚线为geom_smooth()函数绘制,实线为geom_xspline()绘制
+
geom_point(shape=21,size=5,colour="black") +
geom_smooth(se=FALSE, linetype="dashed", size=1) +
ggalt::geom_xspline(size=.8)
+
Example Of geom_xspline()
当然,你也可以通过修改geom_xspline()函数的参数进行,如下:
+
geom_point(shape=21,size=5,colour="black") +
geom_smooth(se=FALSE, linetype="dashed", size=1) +
geom_xspline(spline_shape=1, size=0.8)
+
Example Of geom_xspline(spline_shape=1, size=0.8)
geom_bkde()
这个函数主要绘制核密度估计,代码如下:
ggplot(geyser, aes(x=duration)) +
ggalt::geom_bkde(alpha=1/2) +
labs(
title = "Example of <span style='color:#D20F26'>ggalt::geom_bkde function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_bkde() </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(face = 'bold',size = 12))
Example01 ggalt::geom_bkde()
当然,也可以这样:
set.seed(1492)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200),rnorm(200, mean=.8)))
ggplot(dat, aes(x=rating, fill=cond)) +
ggalt::geom_bkde(alpha=0.7)
+ ···
Example02 ggalt::geom_bkde()
geom_encircle()
这个函数的作用还是蛮大的,它可以实现对特定数据进行圈出标记,如下:
ggplot(mpg, aes(displ, hwy))+
ggalt::geom_encircle(data=subset(mpg, hwy>30),color="red") +
geom_point() +
labs(
title = "Example of <span style='color:#D20F26'>ggalt::geom_encircle function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_encircle() </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(face = 'bold',size = 12))
Example Of ggalt::geom_encircle()
geom_lollipop()和geom_dumbbell()
- geom_lollipop()
df <- read.csv(text="category,pct
Other,0.09
South Asian/South Asian Americans,0.12
Interngenerational/Generational,0.21
S Asian/Asian Americans,0.25
Muslim Observance,0.29
Africa/Pan Africa/African Americans,0.34
Gender Equity,0.34
Disability Advocacy,0.49
European/European Americans,0.52
Veteran,0.54
Pacific Islander/Pacific Islander Americans,0.59
Non-Traditional Students,0.61
Religious Equity,0.64
Caribbean/Caribbean Americans,0.67
Latino/Latina,0.69
Middle Eastern Heritages and Traditions,0.73
Trans-racial Adoptee/Parent,0.76
LBGTQ/Ally,0.79
Mixed Race,0.80
Jewish Heritage/Observance,0.85
International Students,0.87", stringsAsFactors=FALSE, sep=",", header=TRUE)
library(scales)
ggplot(df, aes(y=reorder(category, pct), x=pct))+
geom_lollipop(point.colour="steelblue", point.size=2, horizontal=TRUE) +
scale_x_continuous(expand=c(0,0), labels = scales::percent_format(accuracy = 1),
breaks=seq(0, 1, by=0.2), limits=c(0, 1)) +
labs(
title = "Example of <span style='color:#D20F26'>ggalt::geom_lollipop function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_lollipop() </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(face = 'bold',size = 12))
Example Of ggalt::geom_dumbbell()
注意:这里使用了:
scale_x_continuous(expand=c(0,0), labels = scales::percent_format(accuracy = 1),
breaks=seq(0, 1, by=0.2), limits=c(0, 1))
可以标准化X/Y轴的labels样式
- geom_dumbbell()
# 读取数据和处理数据
health <- read.csv("https://rud.is/dl/zhealth.csv", stringsAsFactors=FALSE,
header=FALSE, col.names=c("pct", "area_id"))
areas <- read.csv("https://rud.is/dl/zarea_trans.csv", stringsAsFactors=FALSE, header=TRUE)
health %>%
mutate(area_id=trunc(area_id)) %>%
arrange(area_id, pct) %>%
mutate(year=rep(c("2014", "2013"), 26),
pct=pct/100) %>%
left_join(areas, "area_id") %>%
mutate(area_name=factor(area_name, levels=unique(area_name))) -> health
setNames(bind_cols(filter(health, year==2014), filter(health, year==2013))[,c(4,1,5)],
c("area_name", "pct_2014", "pct_2013")) -> health
# 可视化绘制
ggplot(health, aes(x=pct_2014, xend=pct_2013, y=area_name, group=area_name)) +
geom_dumbbell(colour="#0072B5", size=2, colour_xend="#BC3C28",
dot_guide=TRUE, dot_guide_size=0.15)+
scale_x_continuous(labels = scales::percent_format(accuracy = 1)) +
labs(
title = "Example of <span style='color:#D20F26'>ggalt::geom_dumbbell function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_dumbbell() </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(face = 'bold',size = 12))
Example01 Of geom_dumbbell()
当然,你也可以修改geom_dumbbell()函数的部分参数,如下:
df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
ggplot(df, aes(y=trt, x=l, xend=r)) +
geom_dumbbell(size=3, color="#868686",
colour_x = "#0073C2", colour_xend = "#EFC000",
dot_guide=TRUE, dot_guide_size=0.25) +
hrbrthemes::theme_ipsum(grid="X",base_family = "Roboto Condensed") +
labs(
title = "Example of <span style='color:#D20F26'>ggalt::geom_dumbbell function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_dumbbell() </span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
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(face = 'bold',size = 12))
Example01 Of geom_dumbbell()
更多关于R-ggalt包的绘制函数可参考:R-ggalt包官网[1]。
总结
今天小编简单介绍了R-ggalt包 的几个常用的绘图函数,特别是其geom_xspline() 提供了多个拟合样式和参数设置,非常的好用,希望对小伙伴们有所帮助~~
参考资料
[1]
R-ggalt包官网: https://github.com/hrbrmstr/ggalt/。
相关文章
- investintech-提供在线docx转换doc的工具
- Get几个非常实用技能——常用文件操作工具类、图片视频操作
- 大数据ClickHouse(二十):ClickHouse 可视化工具操作
- Mac端触控板增强工具:Bettertouchtool Mac下载
- wireshark抓包工具详细说明及操作使用_wireshark ping抓包
- Linux Shell工具篇 - 文本排序工具sort
- 手把手教你封装一个日期格式化的工具函数
- 强大的批量采集挖掘未注册老域名的工具
- 【Android 安全】使用 360 加固宝加固应用 ( 加固工具准备 | 生成签名 APK | 加固操作 | 反编译验证加固效果 )
- 【Windows 逆向】CE 地址遍历工具 ( CE 结构剖析工具 | 尝试进行瞬移操作 | 尝试查找飞天漏洞 )
- MSSQL数据库备份:最佳实践与解决方案(mssql数据库备份工具)
- MySQL:支持数据库操作的强大工具(写数据库mysql)
- 学习Oracle:利用Oracle工具更高效地操作(oracle工具使用教程)
- 高效工具,Redis PHP封装类带来便捷操作(redisphp封装类)
- Redis客户端连接工具,让操作更轻松!(redis客户端连接工具)
- MySQL:只有frm文件,数据恢复强力工具备战(mysql只有frm)
- 「Linux修复引导」工具大集合:易操作、高效快速修复,快速恢复系统正常启动。(linux修复引导工具)
- Redis:为何成为云计算中不可或缺的缓存工具?(redis好处)
- 图形化操作构建Redis集群(图形工具链接redis)
- 警方和GreyShift承诺相互为对方的iPhone解锁工具保密
- C#字符串的常用操作工具类代码分享