rigraphtkplot

Change background color of tkplot (igraph,R)


I currently have a network plotted with tkplot(). Originally, I was saving these plots as pngs, but they were too compressed and I liked how a screen shot of the tkplot looked. Is there anyway to make the background of the plot white? Instead of the light grey.


Solution

  • A couple points to clarify things.

    If the PNG was too crowded, you can create a bigger PNG file, just specify width and height in the png() call. Or you can make the vertices smaller as well.

    The purpose of tkplot() is that sometimes it is just easier to hand-adjust vertex coordinates. The idea is that you call tkplot(), adjust the coordinates, query the adjusted coordinates via tkplot.getcoords(), and then use them with plot(), because plot() is just much more flexible than tkplot().

    It is actually possible to change the background color in tkplot(), here is how:

    library(igraph)
    
    g <- graph.ring(10)
    id <- tkplot(g)
    tkconfigure(igraph:::.tkplot.get(id)$canvas, "bg"="lightyellow")
    

    screenshot

    In the next version of igraph it will be possible to query the canvas via tkplot.canvas(), so you won't need to use the internal igraph:::.tkplot.get() command for this.

    Unfortunately the background color of the canvas is a property of the widget, so when you export the canvas to EPS, it will be ignored. The way to get around this requieres that you plot a big rectangle of the desired color, and place it below the vertices and edges in the canvas. This is definitely possible, but it is just easier to query the coordinates via tkplot.getcoords() and then use plot().