0

I am working with a large network and wish too highlight certain nodes. I would like these nodes to plot on top of a dense network. They currently are identified by a certain color. Here is some simple example code.

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)

enter image description here

I have really exaggerated the dot size here to make them overlap. Is there a way I can get the "yes" points to always plot on top of the "no" points?

EDIT: Added "labels" for clarity.

Henry Holm
  • 495
  • 3
  • 13

1 Answers1

1

Yes, there is! Your color vector first denotes the "yes" and then the "no", which seems to determine the plotting order. Assuming you have more than "yes" or "no", you could try convert the color vector to a factor and set levels. Then you can sort the order of your "yes"s and "no"s:

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)

enter image description here

EDIT 1

As per your comment, I cannot think of a (more) elegant solution, but this works for me:

#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))

The idea is to plot the dots with geom_point() again but only for the dots which are "yes".

EDIT 2

I couldn't help but think of another solution without plotting the points again. It is possible to retrieve the plot information using ggplot_build() and then to reorder the hierarchy of the points drawn; the datapoints which come first are drawn first. Hence doing the following will work:

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)
PLY
  • 531
  • 2
  • 4
  • 18
  • Hey! thanks so much for your reply. This indeed moves the white dots too the front but changes which dots are white. – Henry Holm Nov 22 '20 at 02:32
  • @HenryHolm Are you sure? For my example 'yes' stays white which is the same colour as in your image. – PLY Nov 22 '20 at 02:47
  • Yes - White stays with 'yes' but the dots that are white change color. X1 and X2 are white in the original and X11 and X12 in after the fix. Sorry if my initial question was unclear. – Henry Holm Nov 23 '20 at 01:02
  • @HenryHolm I see; I misinterpreted and didn't check properly. Does the new code work for you? – PLY Nov 23 '20 at 02:57
  • @HenryHolm I added another method which worked for me. – PLY Nov 23 '20 at 12:50
  • thanks so much for both these edits. This works well! Thank you for introducing me to ggplot_build() as well. That is very useful. – Henry Holm Dec 05 '20 at 17:52