Why the graph is not proper in R? - r

I wanted to plot the graph from the adjacency matrix. As a first step, I have tried the following code.
set.seed(1)
library('igraph');
adjm1<-matrix(sample(0:1,100,replace=TRUE,prob=c(0.9,01)),nc=10);
g1<-graph.adjacency(adjm1);
plot(g1)
But it gave me, the following graph.
What is the mistake here?
PS: I am using
Rstudio Version 1.1.442
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

It seems for whatever reason the default arrow size on your system is too large. One can specify the arrow size in the plot statement. Example: edge.arrow.size = 0.5
set.seed(1)
library('igraph');
adjm1<-matrix(sample(0:1,100,replace=TRUE,prob=c(0.9,01)),nc=10);
g1<-graph.adjacency(adjm1);
plot(g, edge.arrow.size = 0.1)
From similar question: igraph - plotting directed network creates triangular edges

Related

Can not customize points in rgl plot3D

I am trying to customize points in my 3D pca chart, but whatever I do, they just remain as points, and their size can not be changed:
with(df_new, plot3d(PC1,PC2,PC3, col = color, type="p", radius = 0.5))
sessionInfo():
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4
Matrix products: default
BLAS:
/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/
vecLib.framework/Versions/A/libBLAS.dylib
LAPACK:
/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/
libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats4 parallel stats graphics grDevices utils
datasets methods base
Any suggestions would be greatly appreciated
I think that you are using radius to change the size of the points, but radius only applies when you print spheres using type="s". To change the size of the points, use size.
with(df_new, plot3d(PC1,PC2,PC3, col = color, type="p", size = 0.5))
Although size=0.5 will be very small.

How to Plot Graphs in Julia 0.5?

I'm learning Julia by working through Chris Rackauckas' Introduction and I've come across a task that requires me to plot some data. I couldn't manage to get the Plots module to import so I tried a simple test:
using Plots
x = 1:10
y = 0.5*x + 3
plot(x, y)
When I first run this piece of code using the Juno IDE I get an error:
LoadError: LoadError: LoadError: syntax: unhandled expr (error #<julia: Main.Base.MethodError(f=FixedPointNumbers.#floattype(), args=(Main.FixedPointNumbers.FixedPoint{UInt8, 8},))>)
in include_from_node1(::String) at .\loading.jl:488 (repeats 2 times)
in eval(::Module, ::Any) at .\boot.jl:234
in require(::Symbol) at .\loading.jl:415
in include_string(::String, ::String) at .\loading.jl:441
in include_string(::Module, ::String, ::String) at 2
This refers to the using statement in my snippet. This error does not appear when I run from the REPL. The version info is as follows:
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
System: NT (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-4810MQ CPU # 2.80GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.7.1 (ORCJIT, haswell)
I currently have version 0.10.3 of Plots installed.
If you provide some version/platform information by sharing the output of versioninfo(), one can help you better.
For instance, the below excerpt
Pkg.add("Plots")
using Plots
plotly() # this backend is installed by default
x = 1:10
y = 0.5*x + 3
plot(x, y)
works well under
Julia Version 0.5.0
Commit 3c9d753* (2016-09-19 18:14 UTC)
Platform Info:
System: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i7-3720QM CPU # 2.60GHz
WORD_SIZE: 64
BLAS: libopenblas (NO_LAPACK NO_LAPACKE NO_AFFINITY SANDYBRIDGE)
LAPACK: liblapack
LIBM: libm
LLVM: libLLVM-3.7.1 (ORCJIT, ivybridge)
Maybe you should conisder Pkg.add("PyPlot") or a similar backend, and try it again later?
To work with the Run File command using the Juno package in the Atom IDE the plot has to be assigned to a variable and passed to the display function.
using Plots
pyplot()
x = 1:100
y = 0.5*x + 10
println(y)
graph = plot(x, y)
display(graph)
This will display the graph in Juno's Plots window. In the comments Arda Aytekin suggested that pyplot(display=true) or graph = plot(x, y, display=true) could be used, which leads to the graph displaying in a separate pyplot window.

R igraph graph density, diameter, eccentricity and shortest path

I hope someone can help me out.
I have an undirected graph g with n=2071 nodes and m=9023 edges.
I calculated the graph density in R using the igraph package and got the following:
> graph.density(g,loop=FALSE)
[1] 0.00210475
However, using the formula for graph density, i.e. density = mean degree / (n-1), I got the following:
> mean(degree(g))/(vcount(g)-1)
[1] 0.0042095
Why does graph.density() give me a different (is it wrong?) answer?
Another question, doesn't the maximum eccentricity = diameter = maximum shortest path of the graph? Or am I confusing the concepts?
Calculating in R using the igraph package, I got the following:
> max(shortest.paths(g,mode="all"))
[1] 17
> diameter(g,directed=FALSE,unconnected=FALSE,weights=NULL)
[1] 17
> max(eccentricity(g,mode="all"))
[1] 8
I used Gephi to double check and I got the diameter=8.
Why is there a disparity between the values?
Also, I found an almost similar question asked before (igraph radius and diameter), but it does not quite ask/answer what I want. It says that the bug has been fixed.
The diameter calculation in igraph considers the edge weights while performing the calculation, so in order to make the calculated diameter same in both igraph and Gephi we need to set weights parameter to 'NA' in igraph.
diameter(graph_data, weights = NA)

How can I increase the map size in a plot?

I want to plot points in an openstreetmap. To determine a suitable range for the map I want to use min() and max() and increase the size by 10%:
library(OpenStreetMap)
coords <- data.frame(cbind(c(-2.121821, -2.118570, -2.124278),
c(51.89437, 51.90330, 51.90469)))
topleft <- c(max(coords[,2]) + 0.1 * max(coords[,2]),
min(coords[,1]) - 0.1 * min(coords[,1]))
bottomright <- c(min(coords[,2]) - 0.1 * min(coords[,2]),
max(coords[,1]) + 0.1 * max(coords[,1]))
map <- openproj(openmap(topleft, bottomright, zoom = "16", type="osm"))
When I now try to create the map R eats up all my resources and I have to kill the process. Is there a better way to achieve this?
R version 3.0.1 (2013-05-16)
Platform: x86_64-unknown-linux-gnu (64-bit)
other attached packages:
[1] ggplot2_0.9.3.1 OpenStreetMap_0.3.1 rgdal_0.8-14 raster_2.2-12 sp_1.0-14
[6] rJava_0.9-6
You're extending the range incorrectly, as you'll see if you have a look at the computed values of topleft and bottomright.
A less error-prone approach might use the function extendrange() (which is used by many R plotting functions to add a little buffer around the most extreme points in the plot).
xx <- extendrange(coords[[1]], f=0.10)
yy <- extendrange(coords[[2]], f=0.10)
tl <- c(max(yy), min(xx))
br <- c(min(yy), max(xx))
map <- openproj(openmap(tl, br, zoom="16", type="osm"))
plot(map)

Help in using rgl package

I installed rgl package with the option --disable-libpng. I tried generating a 3d scatter plot and it crashes. Please help me in resolving this
This is the code i am running
library(rgl)
open3d()
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
It crashes with below messages
*** caught segfault ***
address (nil), cause 'memory not mapped'
Traceback:
1: .External(rgl_par3d, args)
2: par3d(skip)
3: plot3d.default(x, y, z, col = rainbow(1000))
4: plot3d(x, y, z, col = rainbow(1000))
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection:
Here is the information from sessionInfo()
> sessionInfo()
R version 2.11.1 (2010-05-31)
x86_64-unknown-linux-gnu
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rgl_0.92.798
This is from the sysname command
x86-64_linux_2.6.16_ImageSLES10SP3-3
Some more info:
I am able to generate a surface plot from some code in R: Plotting a 3D surface from x, y, z
Here is the code
x <- seq(-10, 10, length.out = 50)
y <- x
rotsinc <- function(x,y) {
sinc <- function(x) {
y <- sin(x)/x;
y[is.na(y)] <- 1;
y
}
10 * sinc( sqrt(x^2+y^2) )
}
z <- outer(x, y, rotsinc)
surface3d(x, y, z)
I tried demo(rgl) and that is also crashing with similar message. I want to generate 3d plots, which other package do you recommend? ggplot?
The rgl package makes use of possible hardware acceleration in your graphics card via its driver.
This is unfortunately entirely dependent on the driver. I have been using rgl for animated visualization for a number of years---see eg this visualization of option analytics surfaces from 2005---which I can assure you crashed for no good reason on some machines and runs on others. You really should try on a different machine with a different driver before making any firm conclusions.
Computers use hardware, and sometimes the hardware bites. I can your code fine on one of my machines. Another is dual-screen and hence without GL extension so it won't. Did I mention hardware bites?
I tested the exact same code on my system, and it worked perfectly.
Whatever the issues were, they have probably been fixed.
Test 1:
library(rgl)
demo(rgl)
Test 2:
library(rgl)
open3d()
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
My system is Windows 7 x64 running R v2.14.2. Tested under two IDEs, namely Revolution R and RStudio.

Resources