接上一節(jié)內(nèi)容败明,繼續(xù)介紹 ggforce 包的使用
2. 線條
線條一般用來(lái)連接點(diǎn)或作為路徑什猖,線條的種類有很多,每種類型的線條提供了不同版本的函數(shù)援所,可以用來(lái)設(shè)置線條的漸變色、線條連接端的類型等
2.1 連接線
連接線有三個(gè)函數(shù): geom_link欣除、geom_link0住拭、geom_link2
geom_link 與 geom_segment 類似,但是通過(guò)在兩個(gè)點(diǎn)之間內(nèi)插許多個(gè)點(diǎn)來(lái)實(shí)現(xiàn)的历帚。其計(jì)算屬性 index 表示點(diǎn)序列,可以用來(lái)將線條繪制成漸變色。
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 基本上是一樣的谅畅,只是沒(méi)有了 index 計(jì)算變量
ggplot(lines) +
geom_link0(aes(x = x, y = y, xend = xend, yend = yend, colour = colour))
geom_link2 與 geom_path 類似彤悔,可以在起點(diǎn)和終點(diǎn)之間內(nèi)插各種圖形屬性,如顏色
ggplot(lines) +
geom_link2(aes(x = x, y = y, colour = colour, size = width, group = 1),
lineend = 'round', n = 500)
2.2 弧線
這組函數(shù)可以根據(jù)圓心禽拔、圓半徑以及起始和終止的角度(弧度)來(lái)繪制一條弧線刘离,可以在笛卡爾坐標(biāo)系中繪制弧線,而不需要使用 coord_polar() 來(lái)轉(zhuǎn)換坐標(biāo)軸
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 計(jì)算變量來(lái)設(shè)置線條的大小和漸變色
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")
0 版本的函數(shù)是直接生成曲線對(duì)象睹栖,而不是根據(jù)點(diǎn)來(lái)計(jì)算的
ggplot(arcs) +
geom_arc0(aes(x0 = 0, y0 = 0, r = r, start = start, end = end,
linetype = factor(r), colour = factor(r)))
2 版本可以在起點(diǎn)和終點(diǎn)之間設(shè)置圖形參數(shù)硫惕,例如添加不同的顏色
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 貝塞爾曲線
這組函數(shù)可以根據(jù)點(diǎn)來(lái)繪制二次或三次貝塞爾曲線。bezier 和 bezier2 兩個(gè)函數(shù)是根據(jù)控制點(diǎn)計(jì)算位置野来,然后使用線來(lái)連接端點(diǎn)恼除,繪制曲線。而 bezier0 是使用 bezierGrob 對(duì)象來(lái)繪制
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)) +
# 繪制端點(diǎn)和控制點(diǎn)
geom_point(aes(x = x, y = y, colour = point))
使用 geom_bezier0 繪制的圖形準(zhǔn)確性更差些
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 來(lái)設(shè)置端點(diǎn)之間的線條顏色
ggplot(beziers) +
geom_bezier2(aes(x = x, y = y, group = type, colour = colour))
2.4 B-splines
與貝塞爾曲線類似梁只,b-splines 也是平滑曲線缚柳,不同之處在于它由一個(gè)控制點(diǎn)向量來(lái)控制的埃脏,而且不需要經(jīng)過(guò)任何控制點(diǎn)。
# 所有點(diǎn)秋忙,路徑經(jīng)過(guò)的點(diǎn)和控制點(diǎn)
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
)
)
# 選出路徑所經(jīng)過(guò)的點(diǎn)
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')
設(shè)置分段顏色
ggplot(paths) +
geom_bspline2(aes(x = x, y = y, group = group, colour = class)) +
geom_point(aes(x = x, y = y), data = cp, color = 'steelblue')
簡(jiǎn)約版
ggplot(paths) +
geom_bspline0(aes(x = x, y = y, group = group)) +
geom_point(aes(x = x, y = y), data = cp, color = 'steelblue')
2.5 水平對(duì)角線
水平對(duì)角線也是一種貝塞爾曲線彩掐,只是把控制點(diǎn)移動(dòng)到了垂直于中心點(diǎn),且沿著 x 或 y 軸方向的某一固定長(zhǎng)度灰追。這種對(duì)角線通常用于樹(shù)狀圖的可視化堵幽,如 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 參數(shù)來(lái)設(shè)置中間區(qū)域的陡峭程度
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 可以繪制螺旋線,沒(méi)啥用弹澎,就是為了玩
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))