how to make a distinct graph plot based on one variable - r

I have a data like this
df<- structure(list(14, FALSE, c(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12,
13, 6), c(0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 0), c(0, 1, 2,
3, 4, 12, 5, 6, 7, 8, 9, 10, 11), c(0, 1, 2, 3, 4, 12, 5, 6,
7, 8, 9, 10, 11), c(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13), c(0, 6, 6, 6, 6, 6, 6, 13, 13, 13, 13, 13, 13, 13, 13
), list(c(1, 0, 1), structure(list(), names = character(0)),
list(name = c("Bestman", "Tera1", "Tera2", "Tera3", "Tera4",
"Tera5", "Tetra", "Brownie1", "Brownie2", "Brownie3", "Brownie4",
"Brownie5", "Brownie6", "Brownie7")), list()), <environment>), class = "igraph")
I can plot it like this
plot(df)
if I want to remove the label , I can do this
plot(df,vertex.label=NA)
but it removes it for all and I want to keep the core
I want to be able to plot it with ggplot , removing the label on each node but leave the main core label on , remove the line around the circles

Your dput is not directly reproducible (igraphs contain an environment which isn't included in a dput). The graph I can recover has two linked central nodes, each with several child nodes.
We can draw graphs with plentiful customization options using the tidygraph and ggraph packages:
library(ggraph)
library(tidygraph)
library(extrafont)
as_tbl_graph(g) %>%
activate(nodes) %>%
mutate(type = ifelse(name %in% c("Bestman", "Tetra"), "root", "branch")) %>%
mutate(group = ifelse(name == "Bestman" | grepl("Tera", name),
"Bestman", "Tera")) %>%
ggraph(layout = "igraph", algorithm = "nicely") +
geom_edge_link(width = 2, alpha = 0.1) +
geom_node_circle(aes(r = ifelse(type == "root", 0.4, 0.1), fill = group),
color = NA) +
geom_node_text(aes(label = ifelse(type == "root", name, "")), size = 5,
color = "gray40", family = "Roboto Condensed", fontface = 2) +
theme_graph() +
coord_equal() +
scale_fill_brewer(palette = "Pastel2", guide = "none")

Related

How to fix invalid vertex id error in tidygraph?

Data
network_data <- list(nodes = structure(list(id = c(0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14), label = c("2892056", "2894543", "2894544",
"2894545", "2894546", "2894547", "2894548", "2894549", "2894550",
"2894551", "2894552", "2894553", "2894554", "2894555", "2894556"
)), row.names = c(NA, -15L), class = "data.frame"), links = structure(list(
from = c(3, 5, 7, 13, 13, 7, 3, 5, 0, 0, 5, 2, 7, 6, 13,
11, 0, 3, 2, 7, 13, 3, 0, 0, 5, 3, 13, 4, 0, 14, 13, 7, 2,
3, 5, 0, 12), to = c(0, 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3,
3, 3, 3, 4, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 11, 12, 12,
12, 13, 13, 13, 13, 13, 14), weight = c(1, 2, 2, 1, 2, 1,
1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 1,
2, 1, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1)), row.names = c(NA, -37L
), class = "data.frame"))
I have this list of nodes and links for building a network. Rather than plotting the network, I want to get the network characteristics such as isolates, reciprocity, etc.
Here's the rest of the code that I'm using to obtain these characteristics:
network_data$nodes <- network_data$nodes %>% select(id, label)
network_data$links <- network_data$links %>% rename(from = source, to = target)
print(network_data$nodes)
print(network_data$links)
SNA <- tidygraph::tbl_graph(
nodes = network_data$nodes,
edges = network_data$links,
directed = T
)
The last line is where it errors out.
Error in (function (edges, n = max(edges), directed = TRUE) :
At structure_generators.c:86 : Invalid (negative) vertex id, Invalid vertex id
I googled the issue and seems like it's pretty prevalent, but none of the methods suggested worked for me. What's different in my data that it's still generating the error, and how can I resolve this error?

How to obtain the tree from igraph object in R?

I have a random directed weighted graph gg, it has the next structure:
gg <-
structure(list(10, TRUE, c(0, 0, 1, 2, 2, 5, 5, 6, 6, 6, 6, 9,
9, 9, 9, 9), c(6, 9, 3, 0, 5, 3, 7, 1, 3, 5, 8, 2, 4, 6, 7, 8
), c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), c(3,
7, 11, 2, 5, 8, 12, 4, 9, 0, 13, 6, 14, 10, 15, 1), c(0, 2, 3,
5, 5, 5, 7, 11, 11, 11, 16), c(0, 1, 2, 3, 6, 7, 9, 11, 13, 15,
16), list(c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("C", "D", "I", "J", "K", "N", "O",
"Q", "S", "T"), color = c("yellow", "red", "red", "red",
"red", "red", "green", "red", "red", "green")), .Names = c("name",
"color")), structure(list(weight = c(0.5, 0.5, 1, 0.333333333333333,
0.333333333333333, 0.333333333333333, 0.333333333333333,
0.25, 0.25, 0.25, 0.25, 0.2, 0.2, 0.2, 0.2, 0.2)), .Names = "weight")),
<environment>), class = "igraph")
I need to find all walks from the root (yellow node) to leaves (red nodes). Leaves defined by (a) edge direction and (b) the distance -- from the root to the leave should be two edges only.
In my case, the root is C and leaves should be D, J, N, S, I, K, Q.
I tried to define the (a) condition only.
root <- "C"
leaves = which(degree(gg, v = V(gg), mode = "out")==0, useNames = T)
leaves
# J K Q S
# 4 5 8 9
plot(gg, layout = layout.reingold.tilford(gg, root=root),
edge.arrow.size=0.2, edge.curved=T,
edge.label = round(E(gg)$weight,2))
Question. How to define the (b) condition and add to leaves set D, N, I, K nodes?
Here's one way to do it: use shortest_paths to get all the vertices that are exactly two edges from the root node.
two.edges.from.root = unlist(sapply(shortest_paths(gg,
from = as.numeric(V(gg)["C"]),
mode = "out")$vpath,
function(x) { if(length(x) == 3) { x[3] } }))

plot (ggplot ?) smooth + color area between 2 curves

I have a question for you please :
My data :
Nb_obs <- as.vector(c( 2, 0, 6, 2, 7, 1, 8, 0, 2, 1, 1, 3, 11, 5, 9, 6, 4, 0, 7, 9))
Nb_obst <- as.vector(c(31, 35, 35, 35, 39, 39, 39, 39, 39, 41, 41, 42, 43, 43, 45, 45, 47, 48, 51, 51))
inf20 <- as.vector(c(2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 4, 3, 5, 4))
sup20 <- as.vector(c(3, 4, 4, 4, 5, 4, 4, 5, 4, 4, 5, 5, 5, 6, 5, 6, 6, 5, 7, 6))
inf40 <- as.vector(c(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 4, 3))
sup40 <- as.vector(c(4, 5, 5, 5, 6, 5, 5, 6, 5, 5, 6, 6, 6, 7, 6, 7, 7, 7, 9, 7))
inf60 <- as.vector(c(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 2))
sup60 <- as.vector(c(5, 6, 6, 6, 8, 7, 7, 7, 7, 7, 7, 7, 8, 9, 8, 9, 9, 9, 11, 9))
inf90 <- as.vector(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1))
sup90 <- as.vector(c(10, 11, 11, 11, 15, 13, 13, 14, 12, 13, 13, 13, 14, 17, 15, 17, 17, 16, 21, 18))
data <- cbind.data.frame(Nb_obs, Nb_obst, inf20, sup20, inf40, sup40, inf60 , sup60, inf90 , sup90)
My plot :
plot(data$Nb_obst, data$Nb_obs, type = "n", xlab = "Number obst", ylab = "number obs", ylim = c(0, 25))
lines(data$Nb_obst, data$inf20, col = "dark red")
lines(data$Nb_obst, data$sup20, col = "dark red")
lines(data$Nb_obst, data$inf40, col = "red")
lines(data$Nb_obst, data$sup40, col = "red")
lines(data$Nb_obst, data$inf60, col = "dark orange")
lines(data$Nb_obst, data$sup60, col = "dark orange")
lines(data$Nb_obst, data$inf90, col = "yellow")
lines(data$Nb_obst, data$sup90, col = "yellow")
My question :
There are two things I'd like to do (and so I think it could be done by ggplot):
In the idea of the graph at the top, the "inf" and "sup" are limits of my model in the IC 20%, then 40%, then 60%, and finally 90%. I would first like to smooth each curve, and then I would like to color the surface between two curves of the same IC, for example that the surface between "data$inf90" and "data$sup90" is yellow, the area between "data$inf60" and "data$60" is orange, etc. And I would like to superimpose each of these colored surfaces + put the good legend please.
Thanks for your help !
Cool question since I had to give myself a crash course in using LOESS for ribbons!
First thing I'm doing is getting the data into a long shape, since that's what ggplot will expect, and since your data has some characteristics that are kind of hidden within values. For example, if you gather into a long shape and have, say a column key, with a value of "inf20" and another of "sup20", those hold more information than you currently have access to, i.e. the measure type is either "inf" or "sup", and the level is 20. You can extract that information out of that column to get columns of measure types ("inf" or "sup") and levels (20, 40, 60, or 90), then map aesthetics onto those variables.
So here I'm getting the data into a long shape, then using spread to make columns of inf and sup, because those will become ymin and ymax for the ribbons. I made level a factor and reversed its levels, because I wanted to change the order of the ribbons being drawn such that the narrow one would come up last and be drawn on top.
library(tidyverse)
data_long <- data %>%
as_tibble() %>%
gather(key = key, value = value, -Nb_obs, -Nb_obst) %>%
mutate(measure = str_extract(key, "\\D+")) %>%
mutate(level = str_extract(key, "\\d+")) %>%
select(-key) %>%
group_by(level, measure) %>%
mutate(row = row_number()) %>%
spread(key = measure, value = value) %>%
ungroup() %>%
mutate(level = as.factor(level) %>% fct_rev())
head(data_long)
#> # A tibble: 6 x 6
#> Nb_obs Nb_obst level row inf sup
#> <dbl> <dbl> <fct> <int> <dbl> <dbl>
#> 1 0 35 20 2 2 4
#> 2 0 35 40 2 2 5
#> 3 0 35 60 2 1 6
#> 4 0 35 90 2 0 11
#> 5 0 39 20 8 3 5
#> 6 0 39 40 8 2 6
ggplot(data_long, aes(x = Nb_obst, ymin = inf, ymax = sup, fill = level)) +
geom_ribbon(alpha = 0.6) +
scale_fill_manual(values = c("20" = "darkred", "40" = "red",
"60" = "darkorange", "90" = "yellow")) +
theme_light()
But it still has the issue of being jagged, so for each level I predicted smoothed values of both inf and sup versus Nb_obst using loess. group_by and do yield a nested data frame, and unnest pulls it back out into a workable form. Feel free to adjust the span parameter, as well as other loess.control parameters that I know very little about.
data_smooth <- data_long %>%
group_by(level) %>%
do(Nb_obst = .$Nb_obst,
inf_smooth = predict(loess(.$inf ~ .$Nb_obst, span = 0.35), .$Nb_obst),
sup_smooth = predict(loess(.$sup ~ .$Nb_obst, span = 0.35), .$Nb_obst)) %>%
unnest()
head(data_smooth)
#> # A tibble: 6 x 4
#> level Nb_obst inf_smooth sup_smooth
#> <fct> <dbl> <dbl> <dbl>
#> 1 90 35 0 11.
#> 2 90 39 0 13.4
#> 3 90 48 0.526 16.7
#> 4 90 39 0 13.4
#> 5 90 41 0 13
#> 6 90 41 0 13
ggplot(data_smooth, aes(x = Nb_obst, ymin = inf_smooth, ymax = sup_smooth, fill = level)) +
geom_ribbon(alpha = 0.6) +
scale_fill_manual(values = c("20" = "darkred", "40" = "red",
"60" = "darkorange", "90" = "yellow")) +
theme_light()
Created on 2018-05-26 by the reprex package (v0.2.0).
This produces the plot with shaded areas using base R graphics.
The trick is to pair the x values with the y values.
plot(data$Nb_obst, data$Nb_obs, type = "n", xlab = "Number obst", ylab = "number obs", ylim = c(0, 25))
lines(data$Nb_obst, data$inf20, col = "dark red")
lines(data$Nb_obst, data$sup20, col = "dark red")
lines(data$Nb_obst, data$inf40, col = "red")
lines(data$Nb_obst, data$sup40, col = "red")
lines(data$Nb_obst, data$inf60, col = "dark orange")
lines(data$Nb_obst, data$sup60, col = "dark orange")
lines(data$Nb_obst, data$inf90, col = "yellow")
lines(data$Nb_obst, data$sup90, col = "yellow")
with(data, polygon(c(Nb_obst, rev(Nb_obst)), c(inf90, rev(sup90)), col = "yellow"))
with(data, polygon(c(Nb_obst, rev(Nb_obst)), c(inf60, rev(sup60)), col = "dark orange"))
with(data, polygon(c(Nb_obst, rev(Nb_obst)), c(inf40, rev(sup40)), col = "red"))
with(data, polygon(c(Nb_obst, rev(Nb_obst)), c(inf20, rev(sup20)), col = "dark red"))
The code for a ggplot graph is a bit longer. There is a function geom_ribbon perfect for this.
g <- ggplot(data)
g + geom_ribbon(aes(x = Nb_obst, ymin = sup60, ymax = sup90), fill = "yellow") +
geom_ribbon(aes(x = Nb_obst, ymin = sup40, ymax = sup60), fill = "dark orange") +
geom_ribbon(aes(x = Nb_obst, ymin = sup20, ymax = sup40), fill = "red") +
geom_ribbon(aes(x = Nb_obst, ymin = inf20, ymax = sup20), fill = "dark red") +
geom_ribbon(aes(x = Nb_obst, ymin = inf40, ymax = inf20), fill = "red") +
geom_ribbon(aes(x = Nb_obst, ymin = inf60, ymax = inf40), fill = "dark orange") +
geom_ribbon(aes(x = Nb_obst, ymin = inf90, ymax = inf60), fill = "yellow")
Data.
I will redo your dataset, simplifying its creation. You don't need as.vector and if you are creating a data.frame there is no need for the data.frame method of cbind, data.frame(.) is enough.
Nb_obs <- c( 2, 0, 6, 2, 7, 1, 8, 0, 2, 1, 1, 3, 11, 5, 9, 6, 4, 0, 7, 9)
Nb_obst <- c(31, 35, 35, 35, 39, 39, 39, 39, 39, 41, 41, 42, 43, 43, 45, 45, 47, 48, 51, 51)
inf20 <- c(2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 4, 3, 5, 4)
sup20 <- c(3, 4, 4, 4, 5, 4, 4, 5, 4, 4, 5, 5, 5, 6, 5, 6, 6, 5, 7, 6)
inf40 <- c(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 4, 3)
sup40 <- c(4, 5, 5, 5, 6, 5, 5, 6, 5, 5, 6, 6, 6, 7, 6, 7, 7, 7, 9, 7)
inf60 <- c(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 2)
sup60 <- c(5, 6, 6, 6, 8, 7, 7, 7, 7, 7, 7, 7, 8, 9, 8, 9, 9, 9, 11, 9)
inf90 <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1)
sup90 <- c(10, 11, 11, 11, 15, 13, 13, 14, 12, 13, 13, 13, 14, 17, 15, 17, 17, 16, 21, 18)
data <- data.frame(Nb_obs, Nb_obst, inf20, sup20, inf40, sup40, inf60 , sup60, inf90 , sup90)

"Object 'freq' not found" error applying colour in UpSetR

If I run this reprex, I get the required output:
``` r
library(UpSetR)
listInput <- list(one = c(1, 2, 3, 5, 7, 8, 11, 12, 13),
two = c(1, 2, 4, 5, 10),
three = c(1, 5, 6, 7, 8, 9, 10, 12, 13))
upset(fromList(listInput), order.by = "freq")
```
If I apply a colour, I get the following error.
``` r
library(UpSetR)
listInput <- list(one = c(1, 2, 3, 5, 7, 8, 11, 12, 13),
two = c(1, 2, 4, 5, 10),
three = c(1, 5, 6, 7, 8, 9, 10, 12, 13))
upset(fromList(listInput), order.by = "freq",
queries = list(list(query = intersects, params = list("one"), color = "orange", active = T)))
#> Error in eval(expr, envir, enclos): object 'freq' not found
```
I've looked at the colouring "example 5" in the vignettes, but can't spot my misstep.
Add a column of integers to the dataframe inputted into upset.
library(UpSetR)
listInput <- list(one = c(1, 2, 3, 5, 7, 8, 11, 12, 13),
two = c(1, 2, 4, 5, 10),
three = c(1, 5, 6, 7, 8, 9, 10, 12, 13))
df <- fromList(listInput)
df$n <- sample(1:nrow(df))
upset(df, order.by = "freq",
queries = list(list(query = intersects,
params = list("one"),
color = "orange")))

Passing smooth line through all data points with more than 50 points

I have data that looks like:
year mean.streak
1958 2.142857
1959 3.066667
1960 2.166667
1961 2.190476
The code for my plot with localized regression looks like:
ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+
geom_point(color = 'black')+
geom_smooth(method = 'loess')
and outputs:
I'd like to capture the somewhat sinusoidal pattern of the data by passing a smooth line through all of the data points, rather than the typical jagged geom_line. I tried polynomial interpolation by writing:
ggplot(df)+
geom_point(aes(x = year, y = mean.streak, colour = year), size = 3) +
stat_smooth(aes(x = year, y = mean.streak), method = "lm",
formula = y ~ poly(x, 57), se = FALSE)
Taken from this thread. But I get the error:
Warning message:
Computation failed in `stat_smooth()`:
'degree' must be less than number of unique points
seemingly because there are too many datapoints, as this answer seems to indicate.
Is there a way to pass a smooth line through all the data with 59 data points?
Full data is:
structure(list(year = 1958:2016, mean.streak = c(2.14285714285714,
3.06666666666667, 2.16666666666667, 2.19047619047619, 2.35, 2.42857142857143,
2.28571428571429, 1.92592592592593, 1.69230769230769, 2.61111111111111,
3, 2.94117647058824, 2.2, 2.5, 2.13636363636364, 1.76923076923077,
1.36111111111111, 1.41176470588235, 1.76, 2, 2.63157894736842,
2.08695652173913, 2.86666666666667, 2.125, 3, 3.125, 2.57894736842105,
1.84, 1.46666666666667, 1.7037037037037, 1.625, 1.67741935483871,
1.84, 1.6, 3, 3.11111111111111, 3.66666666666667, 4.18181818181818,
2.85714285714286, 3.66666666666667, 2.66666666666667, 2.92857142857143,
3.1875, 2.76923076923077, 5.375, 5.18181818181818, 4.08333333333333,
6.85714285714286, 2.77777777777778, 2.76470588235294, 3.15384615384615,
3.83333333333333, 3.06666666666667, 3.07692307692308, 4.41666666666667,
4.9, 5.22222222222222, 5, 5.27272727272727), median.streak = c(1,
3, 1.5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2,
2, 3, 2, 2, 2.5, 2, 2, 1, 1, 1, 1, 1, 1, 1.5, 2, 4, 4, 1, 3,
2, 2.5, 2, 2, 5.5, 4, 2.5, 9, 2, 2, 2, 1.5, 2, 3, 2.5, 4.5, 4,
5, 4), max.streak = c(6, 6, 9, 7, 5, 5, 7, 4, 3, 7, 9, 7, 6,
6, 6, 4, 3, 4, 4, 10, 8, 6, 6, 5, 10, 8, 5, 6, 3, 4, 4, 4, 4,
5, 8, 8, 11, 8, 8, 11, 10, 5, 12, 7, 10, 12, 12, 10, 7, 10, 10,
14, 9, 7, 9, 12, 10, 14, 12), mean.std = c(-0.73762950487994,
-0.480997734887942, -0.517355702126398, -0.387678832192802, -0.315808940316265,
-0.455313725347534, -0.520453518496716, -0.598412265824216, -0.523171795723798,
-0.62285788065637, -0.54170040191883, -0.590289727314622, -0.468222025966258,
-0.639180735884434, -0.656427002478427, -0.565745564840106, -0.473399411312895,
-0.564475310127763, -0.493531273810312, -0.543209721496256, -0.640240670332106,
-0.510337503791441, -0.596096374402028, -0.504696265560619, -0.620412635042488,
-0.497008319856979, -0.546623513153538, -0.613345407826292, -0.564945850817486,
-0.581770706442245, -0.5709080560492, -0.627986564445679, -0.680973485641403,
-0.548092447365696, -0.554620596559388, -0.483847268000936, -0.67619820292833,
-0.613245144944101, -0.509832316970819, -0.302654541906113, -0.623276311320811,
-0.431421947082012, -0.525548788393688, -0.244995094473986, -0.412444188256097,
-0.112114155982405, -0.299486359079708, -0.300201791042539, -0.240281366191648,
-0.359719754440627, -0.511417389357902, -0.474906675611613, -0.312106332395495,
-0.449137693833681, -0.526248555772371, -0.56052848268042, -0.390017880007091,
-0.537267264953157, -0.444528236868953)), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("year", "mean.streak", "median.streak",
"max.streak", "mean.std"), row.names = c(NA, -59L))
Adjust the span:
ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+
geom_point(color = 'black')+
stat_smooth(method = 'loess', span = 0.3)
Or use a spline:
library(splines)
ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+
geom_point(color = 'black')+
stat_smooth(method = 'lm', formula = y ~ ns(x, 10))
Generally, you don't want to fit an extremely high-degree polynomial. Such fits look awful. It would be much better to fit an actual time series model to your data:
library(forecast)
library(zoo)
ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+
geom_point(color = 'black')+
geom_line(data = data.frame(year = sort(streaks$year),
mean.streak = fitted(auto.arima(zoo(streaks$mean.streak,
order.by = streaks$year)))),
show.legend = FALSE)

Resources