How to fix invalid vertex id error in tidygraph? - r

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?

Related

How to create ggplot graphs with the three groups into one plot?

My codes are:
ggplot(data=df2, aes(x=stress, fill=as.factor(JP_Gender))) + geom_density(alpha=.3)
ggplot(data=df1, aes(x=CGstress)) + geom_density(alpha=.3)
My dataset 1:
structure(list(CGstress = c(4, 1, 10, 8, 9.5, 5, 5, 6, 6, 6,
7, 3, 4.5, 8, 9, 1, 5, 1, 5.5, 4, 1, 7, 9, 8, 3, NA, 10, 9, 5,
3, NA, 10, 6, NA, 10, 7)), row.names = c(NA, -36L), class = c("tbl_df",
"tbl", "data.frame"))
My dataset 2:
structure(list(stress = c(7, 2, 5, 6, 7, 1, 6, 10, 9, 10, 10,
10, 10, 8, 9, 4, 7, 6, 4, 9, 4, 8, 3.5, 7, 6, 6, 1, 7, 9, 8,
10, 6, 3, 1, 1, 1, 9, 6, 4), JP_Gender = structure(c(1, 2, 1,
2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1), label = "What is your gender?", format.stata = "%12.0g", labels = c(Male = 1,
Female = 2, Transgender = 3, Other = 4), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -39L), class = c("tbl_df",
"tbl", "data.frame"))
Above codes give me 2 graphs. How to combine 2 graphs into one plot? And how to label the legends?
You can try combining the two datasets and then plot :
library(dplyr)
library(ggplot2)
df1 %>%
mutate(id = 3) %>%
rename(stress = CGstress) %>%
bind_rows(df2 %>%
mutate(id = as.integer(JP_Gender)) %>%
select(stress, id)) %>%
mutate(id = factor(id)) %>%
ggplot(aes(x=stress, fill=factor(id))) + geom_density(alpha=.3)

Plotly does not plot a non-quadratic surface

I want to produce a 3D scatterplot and add a surface fitted with a linear regression, using plotly. My data:
structure(list(political_trust = c(1, 6, 7, 5, 0, 2, 1, 3, 5,
0, 2, 5, 5, 6, 6, 3, 3, 2, 5, 8, 3, 7, 3, 4, 5, 4, 5, 0, 0, 4,
6, 1, 0, 4, 0, 5, 5, 6, 7, 3, 5, 4, 5, 2, 4, 4, 7, 6, 7, 5, 4,
6, 7, 5, 7, 3, 3, 3, 2, 5, 2, 7, 3, 2, 7, 2, 3, 0, 7, 5, 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, 0, 0, 3, 2,
3, 4, 0, 4, 4, 4, 4, 3, 0, NA, 4, 0, 4, 3, 4, 1, 5, 2, NA, 0,
0, 4, 3, 3, 5, 3, 4, 0, 4, 4, 0, 4, 5, 4, 2, 2, 0, 5, 3, 0, 4,
1, 5, 4, 0, 4, 4, 5, 5, 4, 4, 4, 5, 2, 3, 2, 4, 0, 4, 0, 5, 4,
4, 4, 4, 4, 4, 2, 4, 5, 3, 4, 3, 0, 4, 4, 4, 3, 4, 4, 0, 3, 4,
2, 3, 3, 0, 4, 4, 4, 5, 4, 0, 4, 4, 4, 0, 3, 1, 4, NA, 4, 0,
1, 2, 4, 0, 2, 1, 4, 4, 4, 3, NA, 5, 2, 1, 0, 0, 4, 3, 3, 4,
3, 0, 3, NA, 4, 0, 0, 4, 5, 4, 5, 2, 2, 0, 3, 4, 3, 1, 3, 2,
3, 5, 0, 4, 5, 0, 5, 2, 0, 3, NA, NA, 2, 4, 3, 4, 3, 2, 2, 4,
4, 3, 0, 4, 0, 4, 4, 3, 0, 4, 4, 3, 5, 0, 3, 0, 4, 3, 0, 3, 3,
3, 4, 5, 1)), row.names = c(NA, -200L), class = "data.frame")
I start by defining a list of relevant variables - this is not necessary but basically a consequence of using the code in a Shiny up:
input <- list()
input$x <- "education_level"
input$y <- "political_trust"
input$z <- "political_interest"
Next, creating the surface data:
# Regressing "political_interest" on "education_level" and "political_trust":
lm <- lm(as.formula(paste0(input$z, " ~ ", input$x, " + ", input$y)), data)
# Defining range of values that outcome will be predicted for
axis_x <- seq(min(data[, input$x], na.rm = T),
max(data[, input$x], na.rm = T), by = 0.2)
axis_y <- seq(min(data[, input$y], na.rm = T),
max(data[, input$y], na.rm = T), by = 0.2)
# Predicting outcome, and getting data into surface format
lm_surface <- expand.grid(x = axis_x, y = axis_y, KEEP.OUT.ATTRS = F)
colnames(lm_surface) <- c(input$x, input$y)
lm_surface <- acast(lm_surface, as.formula(paste0(input$x, " ~ ", input$y)),
value.var = input$z)
Last, plotting this with plotly:
data %>%
filter(!is.na(get(input$z))) %>%
filter(!is.na(get(input$x))) %>%
filter(!is.na(get(input$y))) %>%
plot_ly(., x = ~jitter(get(input$x), factor = 2.5),
y = ~jitter(get(input$y), factor = 2.5),
z = ~jitter(get(input$z), factor = 2.5),
type = "scatter3d", mode = "markers",
marker = list(size = 2, color = "#cccccc")) %>%
add_surface(., z = lm_surface,
x = axis_x,
y = axis_y,
type = "surface")
This gives me the following. As you can see, the surface does not cover the full range of the y-dimension. Note also that the surface plotted is "quadratic" - i.e. same length in x and y - although it should have non-quadratic dimensions.
I can bring plotly to draw larger surface area, e.g. by changing the range of values like below, but it always stays quadratic.
axis_x <- seq(0, 10, by = 0.2)
axis_y <- seq(0, 10, by = 0.2)
Ok, question solved.
It's important which dimension of the surface matrix (lm_surface) is which. Swapping x and y when applying acast fixes the issue:
lm_surface <- acast(lm_surface, as.formula(paste0(input$y, " ~ ", input$x)),
value.var = input$z)

Add legend to graph in R

For a sample dataframe:
df <- structure(list(antibiotic = c(0.828080341411847, 1.52002304506738,
1.31925434545302, 1.66681722567074, 1.17791610945551, 0.950096368502059,
1.10507733691997, 1.0568193215304, 1.03853131016669, 1.02313195567946,
0.868629787234043, 0.902126485349154, 1.12005679002801, 1.88261441540084,
0.137845900627507, 1.07040656448604, 1.41496470588235, 1.30978543173373,
1.16931780610558, 1.05894439450366, 1.24805122785724, 1.21318238007025,
0.497310305098053, 0.872362356327429, 0.902584749481137, 0.999731895498823,
0.907560340983954, 1.05930840957587, 1.40457554864091, 1.09747179272879,
0.944219456216072, 1.10363111431903, 0.974649273935516, 0.989983064420841,
1.14784471036171, 1.17232858907798, 1.44675812720393, 0.727078405331282,
1.36341361598635, 1.06120293299474, 1.06920290856811, 0.711007267992205,
1.39034247642439, 0.710873996527168, 1.30529753573398, 0.781191310196629,
0.921788181250106, 0.932214675722466, 0.752289683770589, 0.942392026874501
), year = c(3, 1, 4, 1, 2, 4, 1, 3, 4, 3, 4, 1, 2, 3, 4, 1, 1,
4, 1, 1, 1, 1, 4, 1, 3, 3, 1, 4, 1, 4, 2, 1, 1, 1, 3, 4, 3, 2,
2, 2, 3, 3, 1, 2, 3, 2, 3, 4, 4, 1), imd.decile = c(8, 2, 5,
5, 4, 3, 2, 8, 6, 4, 3, 6, 9, 2, 5, 3, 5, 6, 4, 2, 9, 11, 2,
8, 3, 5, 7, 8, 7, 4, 9, 7, 6, 4, 8, 10, 5, 6, 6, 11, 6, 4, 2,
4, 10, 8, 2, 8, 4, 3)), .Names = c("antibiotic", "year", "imd.decile"
), row.names = c(17510L, 6566L, 24396L, 2732L, 13684L, 28136L,
1113L, 15308L, 28909L, 21845L, 23440L, 1940L, 8475L, 22406L,
27617L, 4432L, 3411L, 27125L, 6891L, 6564L, 1950L, 5683L, 25240L,
5251L, 20058L, 18068L, 5117L, 29066L, 2807L, 24159L, 12309L,
6044L, 7629L, 2336L, 16583L, 23921L, 17465L, 14911L, 8879L, 13929L,
17409L, 19421L, 7239L, 11570L, 15283L, 8283L, 16246L, 27950L,
23723L, 4411L), class = "data.frame")
I am trying to graph imd.decile by antibiotic for each year
library(ggplot2)
p <- ggplot(df, aes(x = imd.decile, y = antibiotic, group = factor(year))) +
stat_summary(geom = "line", fun.y = mean)
p
How do I add the wave to colour the corresponding graph and add a legend (I can't seem to use the aes command correctly).

Select Input in ggplot Shiny dashboard - Error: object not found

I am trying to create ggplot output using R Markdown Shiny Document. I need it to plot data based on the selection in a dropdown menu. My code:
df<- data.frame(df,out)
renderRpivotTable({
rpivotTable(data = df, rows = c("organisationunitname","X2"), cols = "X1", vals = "value",
aggregatorName = "List Unique Values",inclusions = list(organisationunitname=list("All OUs")),
rendererName = "Lab Table", width = "100%", height = "500px") })
orgunit <- c("Cy3L", "Yieu", "j9ao", "H3LY", "U3nd",
"qU1l", "jXVh", "dXHb", "tCq8", "Blee", "5jra", "qO2V", "Qa9J",
"2XIy", "MJpY", "tNKa", "UorU", "7pZt", "Mxsz", "WCkd", "BiDp",
"Zw8w", "0J7c", "9YtI", "TAkB", "py3Q", "RdQt", "Yhv1", "PB0X",
"H3L4", "INY7", "DpTW", "3zXP", "OqpO", "tiZU", "5wnz")
inputPanel(selectInput("OU", label = "Select OU:", choices = orgunit, selected = "All OUs"))
renderPlot({
df1=reactive({return(df[organisationunitname %in% as.character(input$OU)])})
ggplot(data = df1(),aes(x=X1,y=value))+geom_bar(stat = "identity")+facet_grid(X2~.)
})
It gives me this error: object 'organisationunitname' not found
Error Message
My data:
structure(list(country = c("Cy3L", "Yieu", "j9ao", "H3LY", "U3nd",
"qU1l", "jXVh", "dXHb", "tCq8", "Blee", "5jra", "qO2V", "Qa9J",
"2XIy", "MJpY", "tNKa", "UorU", "7pZt", "Mxsz", "WCkd", "BiDp",
"Zw8w", "0J7c", "9YtI", "TAkB", "py3Q", "RdQt", "Yhv1", "PB0X",
"H3L4", "INY7", "DpTW", "3zXP", "OqpO", "tiZU", "5wnz"), cd4_perform_result = structure(c(24L,
6L, 7L, 1L, 1L, 1L, 5L, 3L, 2L, 1L, 10L, 1L, 2L, 8L, 1L, 2L,
17L, 1L, 1L, 23L, 12L, 1L, 14L, 11L, 18L, 1L, 21L, 16L, 1L, 22L,
19L, 4L, 1L, 15L, 20L, 9L), .Label = c("0", "1", "11", "125",
"130", "14", "15", "194", "24", "261", "27", "31", "3442", "370",
"4", "5", "51", "567", "577", "73", "76", "79", "796", "9", "end"
), class = "factor"), cd4_participate_result = c(1, 8, 8, 1,
1, 1, 5, 3, 2, 1, 7, 1, 2, 9, 1, 2, 17, 1, 1, 18, 12, 1, 4, 15,
14, 1, 20, 16, 1, 21, 10, 6, 1, 19, 13, 3), cd4_pass_result = c(1,
4, 19, 1, 1, 1, 5, 3, 2, 1, 21, 1, 2, 20, 1, 2, 13, 1, 1, 14,
6, 1, 11, 12, 10, 1, 18, 2, 1, 16, 7, 17, 1, 15, 9, 3), eid_perform_result = c(2,
1, 7, 1, 1, 1, 1, 9, 1, 1, 8, 1, 2, 3, 5, 2, 5, 1, 1, 10, 5,
1, 4, 2, 11, 1, 5, 1, 1, 5, 9, 2, 1, 1, 9, 5), eid_participate_result = c(2,
1, 5, 1, 1, 1, 1, 8, 1, 1, 7, 1, 2, 10, 5, 2, 5, 1, 1, 4, 2,
1, 10, 2, 9, 1, 5, 1, 1, 5, 7, 2, 1, 1, 6, 5), eid_pass_result = c(2,
1, 5, 1, 1, 1, 1, 7, 1, 1, 6, 1, 2, 10, 1, 2, 5, 1, 1, 4, 2,
1, 9, 2, 8, 1, 5, 1, 1, 5, 6, 2, 1, 1, 5, 5), vl_perform_result = c(2,
1, 3, 1, 1, 1, 1, 9, 1, 1, 10, 1, 2, 11, 5, 2, 5, 1, 1, 6, 5,
1, 8, 7, 6, 1, 12, 1, 1, 5, 9, 2, 1, 1, 8, 5), vl_participate_result = c(2,
1, 7, 1, 1, 1, 1, 7, 1, 1, 8, 1, 2, 8, 4, 2, 4, 1, 1, 5, 2, 1,
4, 6, 3, 1, 9, 1, 1, 4, 7, 2, 1, 1, 6, 1), vl_pass_result = c(2,
1, 7, 1, 1, 1, 1, 7, 1, 1, 9, 1, 2, 8, 1, 2, 5, 1, 1, 4, 2, 1,
2, 6, 3, 1, 11, 1, 1, 5, 7, 2, 1, 1, 5, 1), hiv_perform_result = c(19,
29, 14, 1, 1, 1, 26, 21, 10, 1, 6, 11, 9, 7, 20, 27, 8, 15, 1,
28, 12, 1, 25, 18, 24, 1, 22, 5, 1, 23, 17, 16, 1, 2, 3, 4),
hiv_participate_result = c(19, 28, 14, 1, 1, 1, 22, 20, 4,
1, 16, 9, 10, 3, 12, 27, 5, 1, 1, 21, 6, 1, 24, 18, 13, 1,
25, 8, 1, 23, 15, 17, 1, 2, 26, 7), hiv_pass_result = c(20,
28, 14, 1, 1, 1, 18, 22, 7, 1, 17, 27, 11, 2, 24, 26, 10,
1, 1, 15, 4, 1, 21, 19, 12, 1, 23, 8, 1, 16, 13, 9, 1, 3,
25, 6), tbafb_perform_result = c(9, 1, 8, 1, 1, 1, 1, 7,
1, 1, 6, 1, 21, 5, 1, 2, 12, 1, 1, 15, 13, 1, 17, 11, 20,
1, 10, 1, 1, 14, 16, 4, 1, 18, 3, 1), tbafb_participate_result = c(1,
1, 18, 1, 1, 1, 1, 5, 1, 1, 12, 1, 19, 11, 1, 2, 6, 1, 1,
13, 7, 1, 10, 9, 14, 1, 8, 1, 1, 16, 15, 4, 1, 18, 3, 1),
tbafb_pass_result = c(1, 1, 19, 1, 1, 1, 1, 6, 1, 1, 13,
1, 20, 11, 1, 2, 4, 1, 1, 15, 5, 1, 7, 10, 12, 1, 8, 1, 1,
16, 9, 3, 1, 14, 18, 1), tbculture_perform_result = c(3,
1, 2, 1, 1, 1, 1, 1, 1, 1, 6, 1, 3, 8, 1, 2, 2, 1, 1, 7,
3, 1, 5, 4, 7, 1, 5, 1, 1, 3, 6, 6, 1, 3, 3, 1), tbculture_participate_result = c(1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 6, 1, 4, 9, 1, 2, 2, 1, 1, 8,
2, 1, 7, 5, 7, 1, 1, 1, 1, 4, 4, 6, 1, 4, 4, 1), tbculture_pass_result = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 4, 8, 1, 2, 2, 1, 1, 9,
2, 1, 7, 5, 6, 1, 1, 1, 1, 4, 4, 7, 1, 4, 4, 1), tbxpert_perform_result = c(1,
1, 4, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 17, 1, 8, 3, 1, 1, 5,
9, 1, 16, 7, 13, 1, 4, 1, 1, 12, 11, 1, 1, 6, 14, 10), tbxpert_participate_result = c(1,
1, 5, 1, 1, 1, 1, 1, 1, 1, 16, 1, 1, 4, 1, 12, 3, 1, 1, 2,
7, 1, 17, 9, 11, 1, 1, 1, 1, 14, 10, 1, 1, 6, 8, 13), tbxpert_pass_result = c(1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 13, 1, 1, 4, 1, 9, 3, 1, 1, 15,
6, 1, 14, 8, 8, 1, 1, 1, 1, 12, 6, 1, 1, 5, 7, 10)), .Names = c("organisationunitname",
"cd4_perform_result", "cd4_participate_result", "cd4_pass_result",
"eid_perform_result", "eid_participate_result", "eid_pass_result",
"vl_perform_result", "vl_participate_result", "vl_pass_result",
"hiv_perform_result", "hiv_participate_result", "hiv_pass_result",
"tbafb_perform_result", "tbafb_participate_result", "tbafb_pass_result",
"tbculture_perform_result", "tbculture_participate_result", "tbculture_pass_result",
"tbxpert_perform_result", "tbxpert_participate_result", "tbxpert_pass_result"
), row.names = c(NA, 36L), class = "data.frame")
I am not sure why it's not reading the "organisationunitname" column. Please help.
I think your error is this line:
df1=reactive({return(df[organisationunitname %in% as.character(input$OU)])})
Change it to:
df1=df[df$organisationunitname %in% as.character(input$OU),])
You also have the incorrect number of dimensions and reactive is not required here because the expression is already in a reactive function: renderPlot.

Error reading dataset in R

I have problem in reading a dataset
My code :
require(igraph)
g <- graph(c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4,
4, 5, 5, 3, 4, 6, 6, 7, 7, 8,
8, 6, 9, 10, 10, 11, 11, 9))
Error :
Error in graph(c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4, 4, 5, 5, 3, 4, 6, 6, 7, :
At structure_generators.c:84 : Invalid (negative) vertex id, Invalid vertex id
The problem seems to be vertex of name 0
yourgraph <- c(0, 1, 1, 2, 2, 0, 1, 3, 3, 4,
4, 5, 5, 3, 4, 6, 6, 7, 7, 8,
8, 6, 9, 10, 10, 11, 11, 9)
g <- graph(yourgraph + 1)

Resources