network plot using gplot with node/point symbols - r

I am trying to produce a black-and-white network diagram using node symbols (point shapes) to differentiate node types, instead of using colors. However, I cannot find a way to do this using the gplot function in the package sna. Here's a simple example:
library(network)
library(sna)
set seed(100)
net <- as.network(matrix(sample(c(0:1),100,replace=TRUE),nrow=10,ncol=10))
symbols <- rep(c(1:2),5)
gplot(net,pch=symbols)
At least with my version of r and sna, gplot just ignores pch. I found documentation here which seems to indicate that at one point vertex.pch could be used to set node symbols. However, this is no longer in the sna documentation and the following code results in an error:
gplot(net,vertex.pch=symbols)
Is there a way to substitute symbols for colors in a network plot, ideally using gplot (I am trying to produce some black and white versions of existing color plots, so I'd rather not start from scratch if possible)?

gplot doesn't directly support pch, but it does let you change the number of sides in the vertex polygon using the vertex.sides argument. So if you wanted to draw your example with triangles and squares:
gplot(net,vertex.col='black',vertex.sides = symbols+2)

Related

Creating a 3D "ribbon" style plot in R

I want to plot the recorded path of an object such as a plane or drone in 3d space using R. The plotted path should be represented by a flat “ribbon” whose orientation perpendicular to the direction of travel changes to reflect the roll of the object. In other words, I want to be able to visualize the path and orientation of the object in one "ribbon" as it banks in turns.
As far as as I can tell, a traditional ribbon plot in R won't allow you to change the orientation in a third dimension.
Matlab and Python have functionality for creating quiver plots where arrows may be placed to indicate the xyz orientation of the object at a given point in time along the path. I cannot find any R packages with similar functionality.
I have played with Plot3D, Plotrgl, Plot3Drgl and cannot find a package that has this capability.
Q1: Are there any packages in R that would support this type of plot?
Q2: If there isn't, how can I go about creating this type of plot R?
Below are examples in Python and Matlab for the 3D quiver type plots, as well as an image demonstrating the ribbon concept I am imagining.
https://medium.com/analytics-vidhya/exploring-data-acquisition-and-trajectory-tracking-with-android-devices-and-python-9fdef38f25ee
Example of 3D quiver plot in python
https://github.com/xioTechnologies/Gait-Tracking-With-x-IMU
Example of 3D quiver plot in matlab
This is an example of what the "ribbon" would look like
Thanks for any help!

Interactive plot in R (part scatterplot, part network)

I am trying to build an interactive plot. It has properties between a scatterplot and a network - I have a list of nodes and edges (network), but I also would like to constrain the nodes, sometimes on the x-axis sometimes on both x- and y- axis (scatterplot). Finally, I have a text label associated with each node that I would like to display (instead of a dot). I was able to create this using ggplot2.
However, some data sets are too large for this to work without the text labels from each node overlapping. Hence, I would now like to add an interactive feature so that the plot consists of dots representing each node, but that upon UI (such as hovering over a dot), the text label belonging to that dot will be revealed.
I would like to achieve this using R.
I tried animint (https://github.com/tdhock/animint) but it seems to mostly allow interaction between two plots, and here I would like to keep it all in one plot.
I also tried htmlwidgets (http://www.htmlwidgets.org/). I looked at two of their packages: I tried using metricsgraphics (mjs_plot), as it has a show_rollover_text option and mouseover option. However, this package does not allow combination of geoms, and so I could not have both dots (nodes) and lines (edges) represented. I also tried network3D package, but that seems to automatically position nodes so that they are distanced far away from each other, and does not seem to provide options to fix each node on a given x and y location.
I am just looking for advice on any other packages I should maybe consider to solve this problem and/or if I may be missing a feature from a package I already tried that could solve this problem. Thank you.
Maybe identify() will be useful for you. But it works only for base plotting system.
x <- rnorm(300)
y <- rnorm(300)
labs <- seq(300)
plot(x,y)
identify(x,y, labels = labs, plot=TRUE)
identify pic

Displaying multiple 3d plots in the same window using plot3D {rasterVis} in R

I am interested in making two 3d topographic plots that display side by side in the same Xquartz device window. Displaying two 3d plots in the same window is straightforward using the rgl package - there are plenty of examples in the documentation using mfrow3d() and other methods.
However, I need to use the plot3D() function from the rasterVis package. The reason I am using plot3D() from rasterVis, rather than something like surface3d() from rgl, is that I need to use the drape argument in plot3D() to display values from a raster as colors on a 3d topographic map (and this raster has different values than the one that creates the z axis on the plot). If anyone has tips on something analogous to drape within an rgl function, I would also be interested in that!
When I try to use mfrow3d() along with the plot3D() function, it brings up a series of blank device windows instead of displaying the two plots side by side in the same window.
Here is some code to make a topographic map using plot3D, from the rasterVis documentation:
data(volcano)
r <- raster(volcano)
extent(r) <- c(0, 610, 0, 870)
levelplot(r, col.regions=terrain.colors)
plot3D(r)
And here is where I try to use mfrow3d to plot 2 identical volcano plots side by side, one in blue and one in red, which I adapted from the rgl documentation:
volcanos <- list(r, r)
col <- c("blue", "red")
open3d()
mfrow3d(1,2)
for (i in 1:2) {
next3d()
plot3D(volcanos[[i]], col=col[i])
}
Is what I am trying to do even possible with plot3D from rasterVis?
The current version of rasterVis::plot3D opens a new device with each call. I have modified its code to test if there is an active device, and open a new one only if needed. With this commit your example works as expected. You should install the development version of rasterVis with devtools::install_github('oscarperpinan/rasterVis').

creating multiple file types while plotting

I would like to produce a series of plots in both high-resolution and low-resolution versions, or stated differently using two different file types (.png and .eps). I'd like to know the best/least repetetive way to do this. I am using the gplot function in sna, and the plot has a custom legend outside the plot area. I wrote a function something like this:
library(sna)
plotfun <- function(net){
png("test.png",width=800)
p <- gplot(net)
par(xpd=T)
legend(max(p[,1])+1,max(p[,2]),legend=letters[1:10],title="custom legend")
dev.off()
seteps()
postscript(test.eps)
#repeat all the plotting commands, which are much longer in real life
dev.off()
}
#try it with some random data
plotfun(rgraph(10))
This is perfectly functional but seems inefficient and clumsy. The more general version of this question is: if for any reason I want to create a plot (including extra layers like my custom legend), store it as an object, and then plot it later, is there a way to do this? Incidentally, this question didn't seem sna specific to me at first, but in trying to reproduce the problem using a similar function with plot, I couldn't get the legend to appear correctly, so this solution to the outside-the-plot-area legend doesn't seem general.
I would recommend generate graphs only in Postscript/PDF from R and then generate bitmaps (e.g. PNG) from the Postscript/PDF using e.g. ImageMagick with -density parameter (http://www.imagemagick.org/script/command-line-options.php#density) set appropriately to get desired resolution. For example
convert -density 100 -quality 100 picture.pdf picture.png
assuming picture.pdf is 7in-by-7in (R defaults) will give you a 700x700 png picture.
With this approach you will not have to worry that the picture comes out formatted differently depending which R device (pdf() vs png()) is used.

Shaded graph/network plot?

I am trying to plot quite large and dense networks (dput here). All I end up with is a bunch of overlapping dots, which does not really give me a sense of the structure or density of the network:
library(sna)
plot(data, mode = "fruchtermanreingold")
However, I have seen plots which utilizes fading to visualize the degree to which points overlap, e.g.:
How can I implement this "fading" in a plot of a graph?
Here's one way:
library(sna)
library(network)
source("modifieddatafromgist.R")
plot.network(data,
vertex.col="#FF000020",
vertex.border="#FF000020",
edge.col="#FFFFFF")
First, I added a data <- to the gist so it could be sourced.
Second, you need to ensure the proper library calls so the object classes are assigned correctly and the proper plot function will be used.
Third, you should use the extra parameters for the fruchtermanreingold layout (which is the default one for plot.network) to expand the area and increase the # of iterations.
Fourth, you should do a set.seed before the plot so folks can reproduce the output example.
Fifth, I deliberately removed cruft so you can see the point overlap, but you can change the alpha for both edges & vertices (and you should change the edge width, too) to get the result you want.
There's a ton of help in ?plot.network to assist you in configuring these options.

Resources