SachaEpskamp / qgraph

Developmental version of qgraph
GNU General Public License v2.0
68 stars 21 forks source link

Using images as nodes without saving as objects #47

Closed heinonmatti closed 4 years ago

heinonmatti commented 4 years ago

Hi,

I have images for nodes, which I'd like to use in a (multiplex network) graph, in a list of lists (i.e. 9 images corresponding to 9 nodes per person, for 19 people). Is there a way to use them as qgraph's node images without first saving them as files?

If not, could you perhaps point to a sensible approach of not having to explicitly list each of the 9*19 images?

Cheers!

heinonmatti commented 4 years ago

I was trying out subplots, but the nodes show up as empty boxes.

Data objects here: matti-analysed.zip

Code:

networks2 <- readRDS("networks.RDS")

recurrence_images2 <- readRDS("recurrence_images.RDS")

qgraph_network <- networks2$edgelist[[12]] %>% 
  qgraph::qgraph(.,
               edgelist = TRUE,
               directed = FALSE,
               label.scale = FALSE,
               layout = "spring",
               lty = .[["line_type"]],
               edge.color = .[["line_colour"]],
               edge.width = .[["line_width"]],
               node.label.offset = c(0.5, -2),
               title = .[["User"]],
               subplots = list(expression(recurrence_images2[[12]][[1]]),
                               expression(recurrence_images2[[12]][[2]]),
                               expression(recurrence_images2[[12]][[3]]),
                               expression(recurrence_images2[[12]][[4]]),
                               expression(recurrence_images2[[12]][[5]]),
                               expression(recurrence_images2[[12]][[6]]),
                               expression(recurrence_images2[[12]][[7]]),
                               expression(recurrence_images2[[12]][[8]]),
                               expression(recurrence_images2[[12]][[9]]))
  )

image

heinonmatti commented 4 years ago

Update: I believe I tried everything, and that you can only use actual plot objects with subplots. So I reverted to trying the approach of saving as images, then using those in the images argument.

I first got a message "Error in readPNG("recurrence_image_9.png") : could not find function "readPNG", which was fixed after I ran readPNG <- png::readPNG. I believe it would've been fixed by library(png), but I don't want to attach packages if it can be avoided.

So, I was able to insert the images by first saving them:

for(i in 1:9){
  ggsave(plot = recurrence_images[[12]][[i]], filename = paste0("recurrence_image_", i, ".png"))
  }

... and then using the line images = c(paste0("recurrence_image_", 1:9, ".png")) on qgraph.

networks2$edgelist[[12]] %>%
  qgraph::qgraph(.,
               edgelist = TRUE,
               directed = FALSE,
               label.scale = FALSE,
               layout = "spring",
               lty = .[["line_type"]],
               edge.color = .[["line_colour"]],
               # edge.color = viridis::inferno(1, begin = 0.5, end = 0.5),
               edge.width = .[["line_width"]],
               # node.label.position = 3, # If offset doesn't work
               node.label.offset = c(0.5, -2.5),
               # borders = FALSE,
               shape = "circle",
               usePCH = TRUE,
               vsize = 12,
               title = .[["User"]],
               images = c(paste0("recurrence_image_", 1:9, ".png")),
               filetype = "png",
               filename = "qgraph_network_recurrencenetworks"
  )

I have two problems left:

  1. Saving the graph as png or jpg leads to poor resolution, whereas pdf is fine. But I need the images to put them up as part of a RMarkdown html site. Any way to increase resolution? usePCH = FALSE and node.resolution = 300 didn't work.

  2. I guess it's not possible to make the nodes circular if the images are rectangles?

Feel free to close this issue if you don't have comments!

image

heinonmatti commented 4 years ago

Update: Using the picture attached (it's not the picture I want, but a circular one I was playing around with), qgraph still gives rectangular nodes, although shape = "circle" :/

circle_thumb5

image

SachaEpskamp commented 4 years ago

This works:

library("png")
library("qgraph")
img <- rep("stroopwafel.png",4)

qgraph(matrix(1,4,4), directed=TRUE,
       images = img, shape = "circle",
       borders = FALSE,
       subplotbg = NA)

with the following image file:

https://www.dropbox.com/s/et8jsojf61uhwk4/stroopwafel.png?dl=0

Best, Sacha