我正在尝试这段代码来重新生成此功能,以基于此链接工作来绘制网络,我知道一些功能已弃用,我将opts替换为theme,但出现theme_rect错误
library(network)
library(ggplot2)
library(sna)
library(ergm)
plotg <- function(net, value=NULL) {
m <- as.matrix.network.adjacency(net) # get sociomatrix
# get coordinates from Fruchterman and Reingold's force-directed placement algorithm.
plotcord <- data.frame(gplot.layout.fruchtermanreingold(m, NULL))
# or get it them from Kamada-Kawai's algorithm:
# plotcord <- data.frame(gplot.layout.kamadakawai(m, NULL))
colnames(plotcord) = c("X1","X2")
edglist <- as.matrix.network.edgelist(net)
edges <- data.frame(plotcord[edglist[,1],], plotcord[edglist[,2],])
plotcord$elements <- as.factor(get.vertex.attribute(net, "elements"))
colnames(edges) <- c("X1","Y1","X2","Y2")
edges$midX <- (edges$X1 + edges$X2) / 2
edges$midY <- (edges$Y1 + edges$Y2) / 2
pnet <- ggplot() +
geom_segment(aes(x=X1, y=Y1, xend = X2, yend = Y2),
data=edges, size = 0.5, colour="grey") +
geom_point(aes(X1, X2,colour=elements), data=plotcord) +
scale_colour_brewer(palette="Set1") +
scale_x_continuous(breaks = NA) + scale_y_continuous(breaks = NA) +
# discard default grid + titles in ggplot2
theme(panel.background = theme_minimal()) + theme(legend.position="none")+
theme(axis.title.x = theme_minimal(), axis.title.y = theme_minimal()) +
theme( legend.background = theme_rect(colour = NA)) +
theme(panel.background = theme_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = theme_minimal(), panel.grid.major = theme_minimal())
return(print(pnet))
}
g <- network(150, directed=FALSE, density=0.03)
classes <- rbinom(150,1,0.5) + rbinom(150,1,0.5) + rbinom(150,1,0.5)
set.vertex.attribute(g, "elements", classes)
plotg(g)
也许您可以将“主题”部分重写为如下形式:
library(network)
library(ggplot2)
library(sna)
library(ergm)
plotg <- function(net, value=NULL) {
m <- as.matrix.network.adjacency(net) # get sociomatrix
# get coordinates from Fruchterman and Reingold's force-directed placement algorithm.
plotcord <- data.frame(gplot.layout.fruchtermanreingold(m, NULL))
# or get it them from Kamada-Kawai's algorithm:
# plotcord <- data.frame(gplot.layout.kamadakawai(m, NULL))
colnames(plotcord) = c("X1","X2")
edglist <- as.matrix.network.edgelist(net)
edges <- data.frame(plotcord[edglist[,1],], plotcord[edglist[,2],])
plotcord$elements <- as.factor(get.vertex.attribute(net, "elements"))
colnames(edges) <- c("X1","Y1","X2","Y2")
edges$midX <- (edges$X1 + edges$X2) / 2
edges$midY <- (edges$Y1 + edges$Y2) / 2
pnet <- ggplot() +
geom_segment(aes(x=X1, y=Y1, xend = X2, yend = Y2),
data=edges, size = 0.5, colour="grey") +
geom_point(aes(X1, X2,colour=elements), data=plotcord) +
scale_colour_brewer(palette="Set1") +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
# discard default grid + titles in ggplot2
theme_minimal() + theme(legend.position="none")+
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA))
return(print(pnet))
}
g <- network(150, directed=FALSE, density=0.03)
classes <- rbinom(150,1,0.5) + rbinom(150,1,0.5) + rbinom(150,1,0.5)
set.vertex.attribute(g, "elements", classes)
plotg(g)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句