R 可视乎|优雅的绘制流程图

简介: R 可视乎|优雅的绘制流程图


代码展示

加载R包

library(tidyverse)
library(igraph)
library(showtext)
library(thematic)
# install.packages("thematic")

构建数据

dat <- tribble(
  ~from, ~to,
  'Are you a horse?', 'No',
  'Are you a horse?', 'Yes',
  'Are you a horse?', 'Maybe',
  'Maybe', 'How many legs\ndo you walk on?',
  'Yes', 'How many legs\ndo you walk on?',
  'No', 'You\'re not a horse',
  'How many legs\ndo you walk on?', 'Two',
  'How many legs\ndo you walk on?', 'Four',
  'Two', 'You\'re not a horse_2',
  'Four', 'Really?',
  'Really?', 'No_2',
  'Really?', 'Yes_2',
  'No_2', 'Can you read\nand write?',
  'Yes_2', 'Can you read\nand write?',
  'Can you read\nand write?', 'Yes_3',
  'Can you read\nand write?', 'No_3',
  'Yes_3', 'You\'re not a horse_3',
  'No_3', 'You\'re reading this,\naren\'t you?',
  'You\'re reading this,\naren\'t you?', 'Yes_4',
  'Yes_4', 'You\'re not a horse_4'
)

创建图形和布局

graph <-  graph_from_data_frame(dat, directed = TRUE)
coords <- graph %>% 
  layout_as_tree() %>% 
  as_tibble(.name_repair = ~c('x', 'y'))
output <- coords %>% 
  mutate(step = vertex_attr(graph, 'name'),label = str_remove(step, '\\_.+'),
         x = -2.5 * x,y = 5 * y,type = case_when(str_detect(label, '\\?') ~ "Question",
      str_detect(step, 'You\'re not a horse') ~ 'Outcome',T ~ 'Answer'))

制作盒子

box_width <- 1.2
box_height <- 1.25
boxes <- output %>%mutate(xmin = x - box_width,xmax = x + box_width,ymin = case_when(
  str_detect(step, '(legs|reading|write)') ~ y - 1.5 * box_height,T ~ y - box_height),
    ymax = case_when(str_detect(step, '(legs|reading|write)') ~ y + 1.5 * box_height,T ~ y + box_height))

制作边文件

edges <- dat %>%
  mutate(id = row_number()) %>%
  pivot_longer(cols = c("from", "to"),names_to = "s_e",values_to = "step") %>%left_join(boxes, by = "step") %>%
  select(-c(label, type, y, xmin, xmax)) %>%
  mutate(y = ifelse(s_e == "from", ymin, ymax)) %>%
  select(-c(ymin, ymax)) %>% 
  mutate(x = case_when(s_e == 'to' & id %in% c(5, 14) ~ x - box_width,T ~ x))
base_colors <- thematic::okabe_ito(2)

数据可视化

ggplot() +geom_path(data = edges,
aes(x,y, group = id),arrow = arrow(length = unit(0.25, 'cm'))) +
  geom_rect(data = boxes,aes(xmin=xmin,xmax = xmax, ymin = ymin, ymax = ymax, fill = type)) +
  geom_text(data = boxes,aes(x = x, y = y,label = label),lineheight = 1) +
  theme_void() +
  theme(legend.position = 'none',
        plot.background = element_rect(fill = 'white', colour = NA)) +
  scale_fill_manual(values = c('Question' = base_colors[1],
                               'Answer' = colorspace::lighten(base_colors[1], 0.5),
                               'Outcome' = colorspace::lighten(base_colors[2], 0.1))) +
  coord_cartesian(xlim = c(-4.5,5))


477fea0b0aa81f5a9c689093d24da2c9.png

目录
相关文章
|
1月前
时标网络图绘制步骤
时标网络图绘制步骤
时标网络图绘制步骤
|
1月前
|
程序员 uml
UML图 | 时序图(顺序、序列图)绘制
UML图 | 时序图(顺序、序列图)绘制
241 0
|
10月前
|
XML 数据可视化 Android开发
流程图绘制
经典工具:Flowable Eclipse Designer
161 0
|
存储 数据可视化 数据库
使用Powerdesigner绘制数据流图
我们要学习怎么绘制数据流图,首先我们要知道数据流图是什么? 其次是怎么用Powerdesiner。 那么我们现在说说数据流图是什么? 数据流图是什么?
606 0
如何做一个俄罗斯方块3:形状控制
今天,我们来继续学习和实现下一个模块:玩家控制形状。在俄罗斯方块游戏中,玩家可以对下落的形状进行控制,控制分为两种,一种是控制形状的移动(左,右,下),一种是控制形状的旋转(顺时针旋转 90 度)。
112 0
|
存储 数据可视化
R可视乎|创建乐高版马赛克图
今日内容比较“无用”,觉得比较好玩,所以就做一期“异类”可视化啦!主要介绍下 brickr[1] 包,它将乐高(LEGO) 带入 R 和 tidyverse 生态系统中,该包分为2个部分: • Mosaics(马赛克)[2]:将图像转换为乐高积木的马赛克图像。 • 3D 模型[3]:使用 rgl 包,通过数据表构建 3D 乐高模型。 今天这一期主要介绍第一个部分:
139 0
R可视乎|创建乐高版马赛克图
|
数据可视化 图形学
R可视乎|圆环图
对于饼图,上一次学习《R语言数据可视化之美》的时候主要利用graphics包和ggplot包(可见R可视乎|饼图)。这几天的学习中发现还有一个更加简便的方法——ggpie包。接下来做简单描述,然后进入圆环图的学习。
442 0
R可视乎|圆环图
|
数据可视化 数据格式
R可视乎|瀑布图
瀑布图(waterfall plot) 用于展示拥有相同的X轴变量数据(如相同的时间序列)、不同的Y轴离散型变量(如不同的类别变量)和Z轴数值变量,可以清晰地展示不同变量之间的数据变化关系。
513 0
R可视乎|瀑布图