其他
ggplot2绘制CNS级热图
欢迎关注R语言数据分析指南
❝本节来介绍如何使用分面来绘制热图并填充特殊字符,下面通过一个小例子来进行展示;数据及代码已上传到小编建立的「VIP群」已经加群的观众老爷可以直接在群内获取,小编的VIP群目前已经上传「公众号文档数据+代码约160余篇」,有需要加群的欢迎扫描文末尾二维码加小编微信「付费99元」后邀请进群,「由于群名额有限人满之后将不在添加新成员」,有需要的请尽早加入,早进早享受;未进群的观众老爷可从文末获取数据
❞
加载R包
library(tidyverse)
library(ggtext)
library(ggforce)
数据清洗
❝此处使用
❞case_when
来根据数值大小进行特殊字符的转换
df <- read_tsv("data.xls") %>%
mutate(
CL_evolution_sign = case_when(
CL_evolution > 0 ~ "↑",
CL_evolution < 0 ~ "↓",
TRUE ~ "="),
PR_evolution_sign = case_when(
PR_evolution > 0 ~ "↑",
PR_evolution < 0 ~ "↓",
TRUE ~ "="))
数据可视化
df %>% ggplot() +
geom_circle(aes( x0= 1, y0 = 1, r = .5, fill = 8-CL), size = .125) +
geom_circle(aes( x0= 2, y0 = 1, r = .5, fill = 8-PR), size = .125) +
geom_text(aes(x = 1, y = 1, label = CL_evolution_sign), color = "#111111", size = 3.5) +
geom_text(aes(x = 2, y = 1, label = PR_evolution_sign), color = "#111111", size = 3.5)+
facet_grid(rows = vars(country), cols =vars(year), switch = "y") + # 设置分面
coord_equal() +
colorspace::scale_fill_continuous_diverging(name=NULL,palette = "Red-Green", mid = 4,
guide = guide_colourbar(barwidth =unit(6, "cm"),barheight = unit(.75, "cm"),
label.theme = element_text(size = 15)),
breaks = c(2,6),labels = c("← Bad","Good →")) + # 图例调整
theme_minimal() +
theme(
text = element_text(),
strip.placement = "outside",
strip.text.y.left = element_text(face = "bold", color = "#111111",size = 12,angle = 0, hjust = 1),
strip.text.x = element_text(face = "bold", color = "#111111", size = 12),
panel.spacing.x = unit(.25, "cm"),
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
legend.direction = "horizontal",
legend.position = "top")
数据获取
❝今天的介绍到此结束,在绘制此图的过程中突然有了一点新的感悟,这个留在下次进行介绍;考虑到部分朋友可能对加小编的VIP群没有兴趣,那么可以分享此篇文档到微信朋友圈30分钟后公众号后台截图给我,也会发送数据给您,如未及时回复请加我微信交流即可
❞