繼@ thelatemail的建議下,我決定把我的編輯到一個答案。我的解決方案基於@ thelatemail的答案。
我寫了一個小函數繪製曲線,這使得使用邏輯函數的:
#Create the function
curveMaker <- function(x1, y1, x2, y2, ...){
curve(plogis(x, scale = 0.08, loc = (x1 + x2) /2) * (y2-y1) + y1,
x1, x2, add = TRUE, ...)
}
工作示例如下。在這個例子中,我想創建一個3級分類的圖:parent
- >2 children
- >20 grandchildren
。一個孩子有12個孫子女,另一個孩子有8個孩子。
#Prepare data:
parent <- c(1, 16)
children <- cbind(2, c(8, 28))
grandchildren <- cbind(3, (1:20)*2-1)
labels <- c("Parent ", paste("Child ", 1:2), paste(" Grandchild", 1:20))
#Make a blank plot canvas
plot(0, type="n", ann = FALSE, xlim = c(0.5, 3.5), ylim = c(0.5, 39.5), axes = FALSE)
#Plot curves
#Parent and children
invisible(mapply(curveMaker,
x1 = parent[ 1 ],
y1 = parent[ 2 ],
x2 = children[ , 1 ],
y2 = children[ , 2 ],
col = gray(0.6, alpha = 0.6), lwd = 1.5))
#Children and grandchildren
invisible(mapply(curveMaker,
x1 = children[ 1, 1 ],
y1 = children[ 1, 2 ],
x2 = grandchildren[ 1:8 , 1 ],
y2 = grandchildren[ 1:8, 2 ],
col = gray(0.6, alpha = 0.6), lwd = 1.5))
invisible(mapply(curveMaker,
x1 = children[ 2, 1 ],
y1 = children[ 2, 2 ],
x2 = grandchildren[ 9:20 , 1 ],
y2 = grandchildren[ 9:20, 2 ],
col = gray(0.6, alpha = 0.6), lwd = 1.5))
#Plot text
text(x = c(parent[1], children[,1], grandchildren[,1]),
y = c(parent[2], children[,2], grandchildren[,2]),
labels = labels,
pos = rep(c(2, 4), c(3, 20)))
#Plot points
points(x = c(parent[1], children[,1], grandchildren[,1]),
y = c(parent[2], children[,2], grandchildren[,2]),
pch = 21, bg = "white", col="#3182bd", lwd=2.5, cex=1)
這是一個不錯的編輯!事實上我可能會偷這個東西。你應該讓你的編輯一個答案,並接受它 - 當然值得upvote或3. – thelatemail
@thelatemail:謝謝你的建議。我添加了我的答案,幷包含了一個稍微詳細的例子。 – Alex