问题
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))
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?
回答1:
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
and4 - (-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 bettervertex.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)
来源:https://stackoverflow.com/questions/11272349/igraph-axes-xlim-ylim-plot-incorrectly