ggplot画图时如何设置线段粗细为连续变化的,这个问题在画网络图时碰到了。我想把边的权重设置为边的粗细,用颜色区分的话效果比较好,但用边的粗细区分就不好了。
library(network)
library(sna)
library(ggplot2)
library(ggnetwork) #基于ggplot2包构建的网络可视化包
n <- network(rgraph(10, tprob = 0.2), directed = TRUE)
e <- sample(-2:2, network.edgecount(n), replace = TRUE)
set.edge.attribute(n, "weight", e)
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
geom_edges(aes(color = weight), curvature = 0.15,
arrow = arrow(length = unit(0.5, "lines"), type = "closed")) +
geom_nodes(size = 3, color = "grey50") +
scale_color_gradient(low = "steelblue", high = "tomato") +
theme_blank() #这是用边的颜色区分权重
ggplot(n, aes(x, y, xend = xend, yend = yend)) +
geom_edges(aes(size = weight), curvature = 0.15,
arrow = arrow(length = unit(0.5, "lines"), type = "closed")) +
geom_nodes(size = 3, color = "grey50") +
scale_color_gradient(low = "steelblue", high = "tomato") +
theme_blank() #这是用边的粗细区分权重,效果不好