R 更改ggnet中的点打印顺序
我正在与一个大型网络合作,我也希望突出显示某些节点。我希望这些节点在密集网络的顶部绘制。它们目前由某种颜色识别。下面是一些简单的示例代码R 更改ggnet中的点打印顺序,r,ggplot2,networking,ggally,R,Ggplot2,Networking,Ggally,我正在与一个大型网络合作,我也希望突出显示某些节点。我希望这些节点在密集网络的顶部绘制。它们目前由某种颜色识别。下面是一些简单的示例代码 library(network) library(GGally) # make a random network x <- c(0,1,0,1,1,1,0,1,0,1,0,1) seed <- c(10,25,40,34,1,35,6,3,14,5,23,3) net <- data.frame(matrix(nrow = 12, ncol
library(network)
library(GGally)
# make a random network
x <- c(0,1,0,1,1,1,0,1,0,1,0,1)
seed <- c(10,25,40,34,1,35,6,3,14,5,23,3)
net <- data.frame(matrix(nrow = 12, ncol = 12))
for (i in 1:12) {
set.seed(seed[i])
net[i] <- sample(x)
}
#plot it with two colors
plot = as.network(net,
directed = FALSE,
ignore.eval = FALSE,
names.eval = 'R_val')
color <- c("yes","yes","no","no","no","no","no","no","no","no","no","no")
final <- ggnet2(net,size = 25,color = color,label = TRUE)
库(网络)
图书馆(GGALY)
#建立一个随机网络
是的,有!您的颜色
向量首先表示“是”,然后表示“否”,这似乎决定了打印顺序。假设您有多个“是”或“否”,您可以尝试将颜色
向量转换为因子并设置级别。然后,您可以按“是”和“否”的顺序排序:
嘿非常感谢您的回复。这确实会将白点移到前面,但会改变哪些点是白色的。@HenryHolm你确定吗?例如,“是”保持白色,与图像中的颜色相同。是-白色保持“是”,但白色的点会改变颜色。X1和X2在原始图像中为白色,而X11和X12在修复后为白色。对不起,如果我最初的问题不清楚的话。@HenryHolm,我明白了;我误解了,没有正确检查。新代码对你有用吗?@HenryHolm我添加了另一个对我有用的方法。
color <- c("yes","yes","no","no","no","no","no","no","no","no","no","no")
factor_color <- sort(factor(color, levels = c("no", "yes")))
ggnet2(net, size = 100, color = factor_color)
#plot it with two colors
plot = as.network(net,
directed = FALSE,
ignore.eval = FALSE,
names.eval = 'R_val')
color <- c("yes","yes","no","no","no","no","no","no","no","no","no","no")
final <- ggnet2(net,size = 100, color = color, label = TRUE)
final_build <- ggplot2::ggplot_build(final)
# Extract the geom_point data and find which elements have 'yes'
yes_index <- which(color == "yes")
label_data <- final_build$data[[2]]
yes_coordinates_label <- cbind(label_data[yes_index,], label = names(net)[yes_index])
final +
geom_point(data = yes_coordinates_label, aes(x = x, y = y),
size = 100, color = first(yes_coordinates_label$colour)) +
geom_text(data = yes_coordinates_label, aes(x = x, y = y, label = label))
library(tidyverse)
# Find the index of the GeomPoint layer
geom_types <- final$layers %>% map("geom") %>% map(class)
GeomPoint_ind <- which(sapply(geom_types, function(x) "GeomPoint" %in% x))
# Retrieve plot information
final_build <- ggplot2::ggplot_build(final)
df <- final_build$data[[GeomPoint_ind]]
# Set the indices which you would like to have on top and modify the ggplot_build object.
yes_index <- which(color == "yes")
final_build$data[[2]] <- rbind(df[-yes_index,], df[yes_index,])
# Convert to plot object and plot
new_final <- ggplot_gtable(final_build)
plot(new_final)