I tried to run this simple rscript on both RStudio and R x64 but nothing appeared as a result. No plot, nothing... Could someone help please?
# Load package
library(networkD3)
# Create fake data
src <- c("A", "A", "A", "A",
"B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
"E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
# Plot
simpleNetwork(networkData)
Code taken by: http://christophergandrud.github.io/networkD3/
I appreciate all your answers! I found a solution that worked to me.
Using the command:
%>% saveNetwork(file = 'File.html')
save an .html file and open it via browser.
Related
I'm new to using graphs in R, and haven't been able to find a solution to this problem. Take a simple graph
library(igraph)
df <- data.frame(a = c("a","a","a","b","c","f"),
b = c("b","c","e","d","d","e"))
my.graph <- graph.data.frame(df, directed = FALSE)
plot(my.graph)
What I want is to be able to define a function which takes the graph and a set of nodes as arguments and for a logical output as to whether those nodes are connected. For example
my.function(my.graph, c("b", "a", "c"))
# TRUE
my.function(my.graph, c("b", "a", "e"))
# TRUE
my.function(my.graph, c("b", "a", "f"))
# FALSE
Any help appreciated
You are just asking if the induced subgraph is connected, so compute the subgraph and test if it is connected.
my.function = function(g, nodes) {
is_connected(subgraph(g, nodes)) }
my.function(my.graph, c("b", "a", "c"))
[1] TRUE
my.function(my.graph, c("b", "a", "e"))
[1] TRUE
my.function(my.graph, c("b", "a", "f"))
[1] FALSE
I have a persp3d plot showing default rates of companies based on the rating (AAA to CCC-) and time (years 1 to 20).
I managed to show a line plot with the axes as numbers, however I would like to have them with the labels AAA to CCC- instead of 5,10,15. I also tried using surface3d() and bbox3d(), but I was not successful.
The code I am using now is:
Y <- c(1:dim(aggregate_row)[2]) # Tenors
X <- c(1:dim(aggregate_row)[1]) # Ratings
Z <- as.matrix.data.frame(aggregate_row)
cc <- colorRamp(rev(rainbow(10)))
Zsc <- (Z-min(Z))/diff(range(Z))
rgbvec2col <- function(x) do.call(rgb,c(as.list(x),list(max=500)))
colvec <- apply(cc(Zsc),1,rgbvec2col)
#surface3d(X,Y,Z,col=colvec)
#bbox3d(color=c("white","black"), xlab=c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "s", "t", "v"))
persp3d(X,Y,Z,col=colvec,xlab="Ratings",ylab="tenor",zlab="Default Rate",front="lines",back="lines")
You didn't include your data, so I'll fake some:
aggregate_row <- matrix(1:400, 20, 20)
The way to get the axes you want is to tell persp3d not to draw axes, and then draw them yourself. For example,
library(rgl)
persp3d(X,Y,Z,col=colvec,xlab="Ratings",ylab="tenor",zlab="Default Rate",front="lines",back="lines",axes=FALSE)
box3d()
axis3d("x") # the default
axis3d("y", at = c(5, 10, 15), labels = c("AAA", "BBB", "CCC"))
axis3d("z-+") # move to a different edge
This produces the following picture:
I have this data.frame and vector:
df <- data.frame (fruit = c(rep("apple", 5), rep("banana", 3), rep("cherry", 6), rep("date", 4)),
letter = c("a", "b", "c", "d", "e", "a", "d", "f", "b", "c", "f", "p", "q", "r", "d", "p",
"x", "y")
)
my_vector <- c("apple", "banana", "date")
Now I would like to use a for loop, which results in vectors with as names the elements in my_vector and as elements those listed in the letter column.
So expected outcome is like this:
apple <- c("a", "b", "c", "d", "e")
banana <- c("a", "d", "f")
date <- c("d", "p", "x", "y")
Thanks you.
We can subset to keep only fruit in my_vector in the data and split it into list of vectors.
list2env(with(subset(df, fruit %in% my_vector),split(letter, fruit)), .GlobalEnv)
apple
#[1] "a" "b" "c" "d" "e"
banana
#[1] "a" "d" "f"
date
#[1] "d" "p" "x" "y"
list2env does write the list of vectors as separate vectors in global environment but usually it is good practice to keep data in the list and not separate them in individual vectors.
A for loop solution would be with assign -
for(vec in my_vector) {
assign(vec, df$letter[df$fruit == vec])
}
Is there a way to colour the edges of the network using the package networkD3 in R? All I have seen is ways of colouring the nodes but I have not seen anything regarding colouring the edges? Thanks
see the help file with ?simpleNetwork
it describes all the possible arguments, including...
linkColour - character string specifying the colour you want the link lines to be. Multiple formats supported (e.g. hexadecimal).
library(networkD3)
Source <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
Target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
NetworkData <- data.frame(Source, Target)
simpleNetwork(NetworkData, linkColour = "green")
I have a small issue about GGVIS in R studio.
I want to plot something and have more information on each point when I move my cursor on it. Thus, I am using GGVIS package and the add_tooltip() function to do it.
However when I run the code below, I obtain the plot but not the additional info when I move my cursor on the points.
Furthermore, I want to use the separate function (tooltip_test) because my real code is a bit more complex and the function would help me a lot.
library(ggvis)
test <- data.frame(ID=1:10, TIME=1:10, COUNTS=rep(1:2,5), EXTRA=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))
tooltip_test <- function(x) {
if (is.null(x)) return(NULL)
if(is.null(x$ID)) return(NULL)
sub_test = test[test$ID == x$ID, ]
paste0("Category: ", sub_test$EXTRA)
}
test %>%
ggvis(x= ~TIME, y= ~COUNTS) %>%
layer_points() %>%
add_tooltip(tooltip_test, "hover")
library(ggvis)
test <- data.frame(ID=1:10, TIME=1:10, COUNTS=rep(1:2,5), EXTRA=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))
tooltip_test <- function(x) {
if (is.null(x)) return(NULL)
paste0('Category: ', test$EXTRA[x$ID])
}
test %>%
ggvis(x= ~TIME, y= ~COUNTS, key := ~ID) %>%
layer_points() %>%
add_tooltip(tooltip_test, "hover")
this should be sufficient for you,u forgot to add ID as key in ggvis implementation