#r #ggplot2
#r #ggplot2
Вопрос:
Я пытаюсь использовать этот код для регенерации этой функции для построения сети на основе этой ссылки, я знаю, что некоторые функции устарели, я заменил 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)
Комментарии:
1. Что произойдет, если вы измените
theme_rect
наelement_rect
?2.
theme_
Похоже, что версии функций устарели еще в версии 1.0 и уже давно удаленыggplot2
. rdocumentation.org/packages/ggplot2/versions/1.0.1/topics /…3. Спасибо @jared_mamrot кажется, это работает, но я не могу это проверить, потому что отображается другая ошибка
Only elements of the same class can be merged
4. Спасибо @MrFlick
Ответ №1:
Возможно, вы могли бы переписать раздел «тема» на что-то вроде этого:
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)
Комментарии:
1. Спасибо @Jared_mamrot Я получаю эту ошибку: недопустимые разрывы спецификации. Используйте NULL, а не NA
2. Я отредактировал свой ответ, чтобы изменить ‘NA’ на ‘NULL’