I was wondering if there is a way to change the edge thickness when using tkplot()
I know you can do it by right clicking the edge and changing it manually, but I would like to be able to call up an attribute to use for the edge. Similar to when using the normal plot function in igraph where I can do edge.width=E(g)$Weight
Also, is there a way to save the tkplot as a png without the use of other packages?
Thanks!
Yes, you can change the edge width, actually it works exactly the same way as for plot().
The Tk canvas does not support the PNG format, so you cannot save tkplot() output in PNG. If you use tkplot() for adjusting coordinates, then use tkplot.getcoords() to query the adjusted coordinates and then use plot() with these coordinates to create the PNG file.
library(igraph)
g <- graph.ring(10)
id <- tkplot(g, edge.width=1:10)
## Now adjust the coordinates by hand, and then continue.
## E.g. I moved vertex 7 to the middle
co <- tkplot.getcoords(id)
png("output.png")
plot(g, layout=co, edge.width=1:10)
dev.off()
Related
I am using R igraph package to display gene networks. The plot on Rstudio is like this (I can't post image because I am new user and don't have enough reputation, sorry about that):
R igraph on preview
Now I want to draw this on file to clearly see the changes and there is always an issue on vertices near margin side like this:
part of output pdf file
My code is as follows`
pdf("graph.pdf",width = 20, height = 10)
par(mar = c(9,9,9,9))
plot(finalnet, edge.arrow.size=0.1, edge.curved=FALSE,vertex.size= 3, margin = -0.5)
dev.off()
Update: I have tried square layout and the problem persists, here is my plotting object and square plot.
square plot
rda file for my igraph object
Can anyone give me an suggestion how to solve this issue? To whole net is about 170 vertices but I don't know why it cannot be displayed on output file well. I have tried different plot options in mai, mar but this seems to fail.
The reason you are getting this behavior is because you are specifying margin in your plot call. margin=-0.5 is telling R to extend the plot 0.5 units past the graphics device dimensions, below are three examples:
Your original plotting call, notice the clipping
pdf("withMargin.pdf")
par(mar=c(9,9,9,9))
plot(g, margin=-0.5)
dev.off()
Without the call to par, problem still presists but now youuse the entire dimension of the graphics device.
png("withoutPar_Margin.png")
#par(mar=c(9,9,9,9))
plot(g, margin=-0.5)
dev.off()
Lastly, removing the margin in plot
png("withoutplotMargin.png")
par(mar=c(9,9,9,9))
plot(g)
dev.off()
You're specifying a rectangular size for what looks like a square object. Try a square size, as in
pdf("graph.pdf")
This will use the defaults, which are square.
But, it's hard to know for sure since you haven't given us the object to troubleshoot for you.
In R, I am generating a large network graph of 100 nodes via the following code:
library(igraph)
data<-data.frame(c(1:500))
relations<-data.frame(from=c(sample(1:500,500,replace=T)),to=c(sample(1:500,500,replace=T)))
g<-graph_from_data_frame(d=relations,vertices = data,directed=T)
l<-layout_with_fr(g)
l <- norm_coords(l, ymin=-2, ymax=2, xmin=-2, xmax=2)
plot(g, edge.arrow.size=.2, edge.curved=0,vertex.color="orange", vertex.frame.color="#555555",vertex.label.color="black",vertex.label.cex=.7,rescale=F,layout=l)
This produces a plot that looks like: https://imgur.com/7v1OqKd
Obviously this is not ideal, as the graph does not fit in the picture. So, I tried exporting the plot to an SVG file via:
svg(width=20, height=20)
plot(g, edge.arrow.size=.2, edge.curved=0,vertex.color="orange", vertex.frame.color="#555555",vertex.label.color="black",vertex.label.cex=.7,rescale=F,layout=l)
dev.off()
This produces an SVG file that appears zoomed in, and I can't zoom out to see the whole graph....even though I didn't adjust the coordinates or scale the graph differently (I believe). Here is a picture: https://imgur.com/hUTKYCZ
How can I export the entire graph nicely to an SVG file? I am not committed to SVG per se but this seemed like a good approach.
EDIT: added igraph library and line to define my layout algorithm
Does changing rescale=T in your plotting function produce what you expect?
plot(g, edge.arrow.size=.2, edge.curved=0,vertex.color="orange", vertex.frame.color="#555555",vertex.label.color="black",vertex.label.cex=.7,rescale=T,layout=l)
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.
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")
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().
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.)
I am using the igraph package in R. My problem is that, when plotting my network using the plot() function, it is not changing in size accordingly to when I change the size of the quartz window. Is there a parameter for this, e.g. similar to stretch="Fill" ?
Another option is to save your plot as SVG or PDF, then crop and resize using an editor such as Inkscape.
Given the comment from the help file quoted here, my suspicion is that you are doing things that igraph is not intended for.
"One convenient way to plot graphs is to plot with tkplot first, handtune the placement of the
vertices, query the coordinates by the tkplot.getcoords function and use them with plot to plot
the graph to any R device."
Either set the quartz window size first, or don't use igraph to create your static plot.
How do I export a re-sized version of the output I get from a call to GraphPlot
(or TreePlot if they produce different output) to a jpg file?
Currently, I'm simply calling Export[file_name, G]
where G is the result from a call to something like GraphPlot.
I'm using Microsoft office picture manager to view the jpgs,
but re-scaling them there yields unsatisfactory results due to poor resolution
(the graph I'm trying to plot has strings as labels which can't be made out after rescaling this way). I would like to be able to choose the size/resolution of the rendered jpg.
As Simon already pointed out, don't use a raster-format for a vector-graphics. Instead, export you plot to e.g. a scalable vector graphics:
graph = GraphPlot[ExampleData[{"Matrix", "HB/can_292"}, "Matrix"]];
Export["graph.svg", graph]
The advantage is, that in such an image, you can still adjust and change lines, polygons and colors. And finally, you can export it to an image of arbitrary quality easily.
And remember, for Plots which contain lines, polygons, ... everything with sharp edges you should never use jpg. General speaking, this is a format for photographs only since its compression is made for reducing filesize in natural images. In those images you don't recognize the compression, in images with text, lines and polygons you certainly will notice the artefacts. Use png if possible. Take your browser and zoom into the above image.
You can set both image size and compression level of the exported file by doing something like
Export[file_name, G, ImageSize -> 1200, "CompressionLevel" -> 0]
The best way I find is to use ImageResolution property. It Increases the resolution of exported image but does not change the scale. Use it like this:
Export[ "image-file-name.png", image, ImageResolution -> 500 ]
Set the size of your graph before exporting it:
Graph[theGraph, ImageSize->2000]