Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

r - igraph axes xlim ylim plot incorrectly

if I make a graph g:

g <- read.table(text="

 A  B   W

 1  55  3
 2  55  5
 3  99  6 ",header=TRUE)

library(igraph)
g <- graph.data.frame(g)

and matrix of coordinates:

y<-1:5
x<-c(0.1,0.1,0.2,0.2,0.8)
l<-data.frame(x,y)
l<-as.matrix(l)

I can plot the graph with node positions according to custom coordinates and plot axes.

plot(g,layout=l,rescale=F,axes=TRUE,ylim=c(0,6),xlim=c(0,1))

graph with wrong axes

But the xaxis limits do not function properly and I think are altered by yaxis limits. How can I control the xaxis they way i want for instance keeping it between 0 and 1.

i.e. plot(x,y,xlim=c(0,1),ylim=c(0,6))

Is this a bug? If it is and this cannot be solved is there another package that would have the same functionality?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The short answer is, you need to set the asp argument of the call to plot to 0 as the default is asp = 1 which produces the behavior you see (i.e., it's not a bug, it's a feature). The long answer with explanation follows.


As you noticed correctly, xaxis varies according to yaxis. Specifically, the x-axis has approxamitely the same distance between high and low numbers as yaxis:

  • If yaxis = c(0,6), the x-axis goes from -3 to 4. 6 - 0 = 6 and 4 - (-3) = 7
  • If yaxis = c(0,3), the x-axis goes from -1 to 2. 3 - 0 = 2 - (-1) = 3

Igraph seems to keep a constant ratio between the axes.

If you call ?plot.igraph (the plotting function called with an igraph object, can also be found via help(package = "igraph")), you find under See Also:

igraph.plotting for the detailed description of the plotting parameters

And if you click on this link (or call ?igraph.plotting)and go through the parameters you will find:

asp A numeric constant, it gives the asp parameter for plot, the aspect ratio. Supply 0 here if you don't want to give an aspect ratio.
It is ignored by tkplot and rglplot.

Defaults to 1.

Hence the aspect parameter asp defaults to 1 in igraph. If you want another ratio, set it to 0:

plot(g,layout=l,rescale=F,axes=TRUE,ylim=c(0,6),xlim=c(0,1), asp = 0)

This answers your question. However, note that the points are now rather big. You will probably want to play around with the following parameters (found on ?igraph.plotting but note that many of the parameters need to be prefixed by vertex. as done by me):

  • vertex.size Default is 15, 5 seems better
  • vertex.label.cex Default is 1, 0.8 seems better.

The following produces a nicer plot:

plot(g,layout=l,rescale=F,axes=TRUE,ylim=c(0,6),xlim=c(0,1), asp = 0, vertex.size = 5, vertex.label.cex = 0.8)

nicer plot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...