I want to use a different size scale for edges and nodes in a network created with the ggnetwork package. If you look at the image below you may think I already did that since they all have different sizes but in fact edge and node size are not scaled independently in this plot. They are both scaled by the scale_size_area()
function towards the same maximum (30). If I get larger nodes my edges will shrink. I guess my problem boils down to scaling the size of different geoms with different functions. So for example how can I scale the size of the nodes with scale_size_area(max_size = 30)
and the size of the edges with scale_size_continuous(range = c(1,6))
?
#load packages
library(network)
library(sna)
library(ggplot2)
library(ggnetwork)
#create data
#data for edges
edge_df<-data.frame(group1=c("A","A","B"),
group2=c("B","C","C"),
connection_strength=c(1,2,3))
#data for nodes/vertexes
vertex_df<-data.frame(group=c("A","B","C"),
groupsize=c(2,3,4))
#create network
my_network<-network(edge_df[,1:2],directed = FALSE)
#add edge attribute (interaction strength) to network object
set.edge.attribute(my_network, "connection_strength", edge_df$connection_strength)
#add node/vertex info to network object with the special %v% operator
my_network %v% "groupsize" = vertex_df$groupsize
#plot
ggplot(my_network, aes(x = x, y = y, xend = xend, yend = yend,color=vertex.names)) +
#edge size depends on connection strength
geom_edges(color = "black",aes(size=connection_strength/20)) +
#node size depends on groupsize
geom_nodes(aes(size=groupsize)) +
#scale size area is good for nodesize but I want a different scaling for the edges
scale_size_area(max_size = 30,guide=F)+
scale_color_discrete(guide=F)+#remove colour legend
scale_x_continuous(limits=c(-0.15,1.15))+#add some space to x-axis
scale_y_continuous(limits=c(-0.15,1.15))+#add some space to y-axis
theme_bw()#simple plot layout
答案 0 :(得分:1)
几乎是this question的副本。
我不是ggplot2专家,但据我所知,双重缩放(例如有两个y轴或两个色标)与图形语法相矛盾。
solution to the aforementioned question可能会有效,但这只是一个黑客攻击。
答案 1 :(得分:1)
我一直在使用scale_edge_width,它允许您将边缘限制为一系列值-有多种口味(连续,手动等),因此添加以下命令会将边缘限制为0.4最多2。
scale_edge_width(range = c(0.4,2))