变化线厚度图

时间:2014-09-18 20:58:04

标签: r plot

是否有一种简单的方法可以在R?

中绘制变化的线厚度图

我有一个数据集(行数约为50k),每个数据点都有一个相关的大小,我想将其绘制为行。假设我的数据如下:

library(data.table)
data.table(x = 1:5, y = 1:5, thickness = c(1:4, NA))
#   x y thickness
#1: 1 1         1
#2: 2 2         2
#3: 3 3         3
#4: 4 4         4
#5: 5 5        NA

我想实现以下目标:

plot(1:2, 1:2, lwd = 1, type = 'l', xlim = c(1,5), ylim = c(1,5))
lines(2:3, 2:3, lwd = 2)
lines(3:4, 3:4, lwd = 3)
lines(4:5, 4:5, lwd = 4)

enter image description here

我知道我可以遍历所有点,只做我上面所做的事情,但宁愿避免这种解决方案。

1 个答案:

答案 0 :(得分:2)

ggplot2解决方案:

library(data.table)
dt <- data.table(x = 1:5, y = 1:5, thickness = c(1:4, NA))

library(ggplot2)
ggplot(dt, aes(x, y, size=thickness)) + geom_path()

enter image description here

相关问题