R 为什么我';“我得到了”;找不到函数“;主题“正确”&引用;在阴谋中?

R 为什么我';“我得到了”;找不到函数“;主题“正确”&引用;在阴谋中?,r,ggplot2,R,Ggplot2,我正在尝试用这段代码重新生成这个函数,以根据这项工作绘制一个网络,我知道有些函数不推荐使用,我用theme替换了opts,但theme_rect出现了错误 library(network) library(ggplot2) library(sna) library(ergm) plotg <- function(net, value=NULL) { m <- as.matrix.network.adjacency(net) # get sociomatrix

我正在尝试用这段代码重新生成这个函数,以根据这项工作绘制一个网络,我知道有些函数不推荐使用,我用theme替换了opts,但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)
库(网络)
图书馆(GG2)
图书馆(sna)
图书馆(ergm)

plotg也许您可以将“主题”部分改写为如下内容:

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)
库(网络)
图书馆(GG2)
图书馆(sna)
图书馆(ergm)

plotg如果将
theme\u rect
更改为
element\u rect
,会发生什么?函数的
theme\u
版本在1.0版中被弃用,看起来像是从
ggplot2
中删除的。谢谢@jared_mamrot它似乎可以工作,但我无法测试它,因为另一个错误显示
只能合并同一类的元素
Thank you@MrFlickThank you@jared_mamrot我得到这个错误:无效的规范。使用NULL,而不是NAI已编辑我的答案以将“NA”更改为“NULL”