我想对使用ggnetwork软件包创建的网络中的边缘和节点使用其他大小比例。如果您看下面的图像,您可能会认为我已经这样做了,因为它们的大小都不同,但是实际上在此图中,边和节点的大小并没有独立缩放。它们都通过scale_size_area()
函数朝着相同的最大值缩放(30)。如果得到更大的节点,则边缘将缩小。我猜我的问题归结为按比例缩放具有不同功能的不同几何图形的大小。因此,例如,如何缩放的大小scale_size_area(max_size = 30)
和的边缘的大小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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句