2014-09-18 54 views
2

有沒有一種簡單的方法來繪製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

我知道我可以遍歷所有的點,只是做我做什麼上面,但寧願避免這種解決方案。

回答

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

+0

很大,這很簡單:)好像'ggplot'不喜歡,去年'NA'(根本不算什麼,增加任何價值有剛剛工作罰款) – eddi 2014-09-18 21:04:10

+0

「NA」被解釋爲不同部分之間的分界線。我認爲這與基礎繪圖中的行爲一致。 – Andrie 2014-09-18 21:06:29