今天小编给大家介绍一下非常优秀的可视化技巧-R-ggalt包绘图技巧,该包的主要特色是提供geom_xspline()、geom_bkde()、geom_encircle()、geom_lollipop()和geom_dumbbell() 等优秀绘图函数,接下来,小编详细介绍这这几个绘图函数,内容如下:
该绘图函数主要用于使用X样条连接控制点的样式,这里和ggplot2的geom_smooth()函数进行比较,首先看一下原始的可视化样式:
# 构建数据
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_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_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)
这个函数主要绘制核密度估计,代码如下:
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()
这个函数的作用还是蛮大的,它可以实现对特定数据进行圈出标记,如下:
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()
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样式
# 读取数据和处理数据
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/。