R 数据可视化 —— ggforce(线条)

接上一节内容,继续介绍 ggforce 包的使用

2. 线条

线条一般用来连接点或作为路径,线条的种类有很多,每种类型的线条提供了不同版本的函数,可以用来设置线条的渐变色、线条连接端的类型等

2.1 连接线

连接线有三个函数: geom_link、geom_link0、geom_link2

geom_link 与 geom_segment 类似,但是通过在两个点之间内插许多个点来实现的。其计算属性 index 表示点序列,可以用来将线条绘制成渐变色。

lines <- data.frame(
  x = c(5, 12, 15, 9, 6),
  y = c(17, 20, 4, 15, 5),
  xend = c(19, 17, 2, 9, 5),
  yend = c(10, 18, 7, 12, 1),
  width = c(1, 10, 6, 2, 3),
  colour = letters[1:5]
)

ggplot(lines) +
  geom_link(aes(x = x, y = y, xend = xend, yend = yend, colour = colour,
                alpha = stat(index), size = stat(index)))

geom_link0 与 geom_segment 基本上是一样的,只是没有了 index 计算变量

ggplot(lines) +
  geom_link0(aes(x = x, y = y, xend = xend, yend = yend, colour = colour))
image

geom_link2 与 geom_path 类似,可以在起点和终点之间内插各种图形属性,如颜色

ggplot(lines) +
  geom_link2(aes(x = x, y = y, colour = colour, size = width, group = 1),
             lineend = 'round', n = 500)
image

2.2 弧线

这组函数可以根据圆心、圆半径以及起始和终止的角度(弧度)来绘制一条弧线,可以在笛卡尔坐标系中绘制弧线,而不需要使用 coord_polar() 来转换坐标轴

arcs <- data.frame(
  start = seq(0, 2 * pi, length.out = 11)[-11],
  end = seq(0, 2 * pi, length.out = 11)[-1],
  r = rep(1:2, 5)
)

# Behold the arcs
ggplot(arcs) +
  geom_arc(aes(x0 = 0, y0 = 0, r = r, start = start, end = end,
               linetype = factor(r)))

使用 index 计算变量来设置线条的大小和渐变色

ggplot(arcs) +
  geom_arc(aes(x0 = 0, y0 = 0, r = r, start = start, end = end,
               size = stat(index), colour = stat(index)), lineend = 'round') +
  scale_radius() +
  scale_colour_gradient(low = "#f7fcf0", high = "#084081")
image

0 版本的函数是直接生成曲线对象,而不是根据点来计算的

ggplot(arcs) +
  geom_arc0(aes(x0 = 0, y0 = 0, r = r, start = start, end = end,
                linetype = factor(r), colour = factor(r)))

2 版本可以在起点和终点之间设置图形参数,例如添加不同的颜色

arcs2 <- data.frame(
  angle = c(arcs$start, arcs$end),
  r = rep(arcs$r, 2),
  group = rep(1:10, 2),
  colour = sample(letters[1:5], 20, TRUE)
)

ggplot(arcs2) +
  geom_arc2(aes(x0 = 0, y0 = 0, r = r, end = angle, group = group,
                colour = colour), size = 2)

2.3 贝塞尔曲线

这组函数可以根据点来绘制二次或三次贝塞尔曲线。bezier 和 bezier2 两个函数是根据控制点计算位置,然后使用线来连接端点,绘制曲线。而 bezier0 是使用 bezierGrob 对象来绘制

beziers <- data.frame(
  x = c(1, 2, 3, 4, 4, 6, 6),
  y = c(0, 2, 0, 0, 2, 2, 0),
  type = rep(c('cubic', 'quadratic'), c(3, 4)),
  point = c('end', 'control', 'end', 'end', 'control', 'control', 'end'),
  colour = letters[1:7]
)

help_lines <- data.frame(
  x = c(1, 3, 4, 6),
  xend = c(2, 2, 4, 6),
  y = 0,
  yend = 2
)

ggplot(beziers) +
  # 添加控制线
  geom_segment(aes(x = x, xend = xend, y = y, yend = yend),
               data = help_lines,
               arrow = arrow(length = unit(c(0, 0, 0.5, 0.5), 'cm')),
               colour = 'grey') +
  geom_bezier(aes(x = x, y = y, group = type, linetype = type)) +
  # 绘制端点和控制点
  geom_point(aes(x = x, y = y, colour = point))

使用 geom_bezier0 绘制的图形准确性更差些

ggplot(beziers) +
  geom_segment(aes(x = x, xend = xend, y = y, yend = yend),
               data = help_lines,
               arrow = arrow(length = unit(c(0, 0, 0.5, 0.5), 'cm')),
               colour = 'grey') +
  geom_bezier0(aes(x = x, y = y, group = type, linetype = type)) +
  geom_point(aes(x = x, y = y, colour = point))

使用 geom_bezier2 来设置端点之间的线条颜色

ggplot(beziers) +
  geom_bezier2(aes(x = x, y = y, group = type, colour = colour))

2.4 B-splines

与贝塞尔曲线类似,b-splines 也是平滑曲线,不同之处在于它由一个控制点向量来控制的,而且不需要经过任何控制点。

# 所有点,路径经过的点和控制点
cp <- data.frame(
  x = c(
    0, -5, -5, 5, 5, 2.5, 5, 7.5, 5, 2.5, 5, 7.5, 5, -2.5, -5, -7.5, -5,
    -2.5, -5, -7.5, -5
  ),
  y = c(
    0, -5, 5, -5, 5, 5, 7.5, 5, 2.5, -5, -7.5, -5, -2.5, 5, 7.5, 5, 2.5,
    -5, -7.5, -5, -2.5
  ),
  class = sample(letters[1:3], 21, replace = TRUE)
)

# 路径
paths <- data.frame(
  ind = c(
    7, 5, 8, 8, 5, 9, 9, 5, 6, 6, 5, 7, 7, 5, 1, 3, 15, 8, 5, 1, 3, 17, 9, 5,
    1, 2, 19, 6, 5, 1, 4, 12, 7, 5, 1, 4, 10, 6, 5, 1, 2, 20
  ),
  group = c(
    1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7,
    7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10
  )
)
# 选出路径所经过的点
paths$x <- cp$x[paths$ind]
paths$y <- cp$y[paths$ind]
paths$class <- cp$class[paths$ind]

ggplot(paths) +
  geom_bspline(aes(x = x, y = y, group = group, colour = ..index..)) +
  geom_point(aes(x = x, y = y), data = cp, color = 'orange')

设置分段颜色

ggplot(paths) +
  geom_bspline2(aes(x = x, y = y, group = group, colour = class)) +
  geom_point(aes(x = x, y = y), data = cp, color = 'steelblue')

简约版

ggplot(paths) +
  geom_bspline0(aes(x = x, y = y, group = group)) +
  geom_point(aes(x = x, y = y), data = cp, color = 'steelblue')

2.5 水平对角线

水平对角线也是一种贝塞尔曲线,只是把控制点移动到了垂直于中心点,且沿着 x 或 y 轴方向的某一固定长度。这种对角线通常用于树状图的可视化,如 ggraph 的 geom_edge_diagonal

data <- data.frame(
  x = rep(0, 10),
  y = 1:10,
  xend = 1:10,
  yend = 2:11
)

ggplot(data) +
  geom_diagonal(aes(x, y, xend = xend, yend = yend))
ggplot(data) +
  geom_diagonal(aes(x, y, xend = xend, yend = yend, alpha = stat(index)))
data2 <- data.frame(
  x = c(data$x, data$xend),
  y = c(data$y, data$yend),
  group = rep(1:10, 2),
  colour = sample(letters[1:5], 20, TRUE)
)
ggplot(data2) +
  geom_diagonal2(aes(x, y, group = group, colour = colour))

使用 strength 参数来设置中间区域的陡峭程度

ggplot(data, aes(x, y, xend = xend, yend = yend)) +
  geom_diagonal(strength = 0.75, colour = 'red') +
  geom_diagonal(strength = 0.25, colour = 'blue')

2.6 螺旋线

geom_spiro 可以绘制螺旋线,没啥用,就是为了玩

ggplot() +
  geom_spiro(aes(R = 10, r = 3, d = 5))

只画一部分,犹抱琵琶半遮面

ggplot() +
  geom_spiro(aes(R = 10, r = 3, d = 5), revolutions = 1.2)

向外旋

ggplot() +
  geom_spiro(aes(R = 10, r = 3, d = 5, outer = TRUE))

你可能感兴趣的:(R 数据可视化 —— ggforce(线条))