Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background color of tkplot (igraph,R)

Tags:

r

igraph

tkplot

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.

like image 218
Judy Avatar asked Oct 17 '25 18:10

Judy


2 Answers

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().

like image 141
Gabor Csardi Avatar answered Oct 19 '25 09:10

Gabor Csardi


There is no color in the background of tkplot-produced objects. It is designed to output eps-postscript files which are basicall transparent and designed to be overlayed on some other file. Open the output of an exported file in a reader and you will see no color in the background. Here's what you get when your run the example on the tkplot page, save as an eps file and open with Preview.app on a Mac. (You should be able to use Ghostscript or ImageMagick for similar display.)

enter image description here

like image 32
IRTFM Avatar answered Oct 19 '25 09:10

IRTFM