tilltnet / egor

R Package for importing and analysing ego-centered-network data.
http://egor.tillt.net
GNU Affero General Public License v3.0
23 stars 4 forks source link

Shiny app not plotting correctly with 1 value of attribute #87

Open EmilyLongGlasgow opened 3 months ago

EmilyLongGlasgow commented 3 months ago
Egor plots

I'm having an issue correctly plotting networks when the alters all share the same value (e.g., each alter scores 1 on the attribute, rather than some scoring 0 and some scoring 1). For example, in the attached image, 0 should be yellow, and 1 should be red. But when an ego only has alters with scores of 0, it assigns that as red. I've tried setting the variable as a factor, and that doesn't work.

pilot$alter$support_mh <- as.factor(pilot$alter$support_mh)
mbojan commented 3 months ago

Thanks @EmilyLongGlasgow . Can you please edit the original post by re-uploading the image?

EmilyLongGlasgow commented 3 months ago

@mbojan Updated

mbojan commented 3 months ago

Thanks. Can you please also add the expression you use to generate the figure? Sorry, forgot to ask earlier.

EmilyLongGlasgow commented 3 months ago

I'm just using the shiny app. egor_vis_app(pilot) #pilot is egor object

Then using the point and click to set the vertex colour as my attribute (yes/no for 'support_mh').

mbojan commented 3 months ago

Oh right! Disregard... Sorry.

mbojan commented 3 months ago

Confirmed. @EmilyLongGlasgow, it's a design flaw unfortunately . I'm thinking of a workaround. Do you have both all-0s and all-1s egonets in your data? In other words, would it help if you recode the attribute 0->1 and 1->0?

Technically

Reprex

d <- egor(
  egos = tribble(
    ~ egoID, ~ a,
    1, 0,
    2, 0
  ),
  alters = tribble(
    ~ egoID, ~ alterID, ~ a,
    1, 1, 0,
    1, 2, 1,
    2, 1, 0,
    2, 2, 0
  ),
  aaties = tribble(
    ~ egoID, ~ Source, ~ Target,
    1, 1, 2,
    2, 1, 2
  ),
  ID.vars = list(
    ego = "egoID",
    alter = "alterID",
    source = "Source",
    target = "Target"
  )
)

plot(d, vertex_color_var = "a")

d$alter$a <- factor(d$alter$a, levels = 0:1)

plot(d, vertex_color_var = "a")

The plotting function seems to correctly build the palette using the levels of the attribute, but it receives an object in which the original levels are lost / redefined to values only present in the egonet. It seems it happens either in slicing the egor object or converting it to igraph.

EmilyLongGlasgow commented 3 months ago

Unfortunately, I have networks with all 0s, and networks with all 1s (just a few, but still there).

mbojan commented 3 months ago

I'm afraid that it is not a 5-minute fix to get it working in the Shiny app. However, you should be able to produce something comparable in R with something along the lines of the snippet below.

library(igraph)

glist <- as_igraph(d)

attr_name <- "a"
col <- c("yellow", "red")
layout(matrix(1:4, byrow=TRUE, ncol=2))
for(g in glist) {
  plot(
    g,
    vertex.label=NA,
    vertex.color = col[get.vertex.attribute(g, attr_name) + 1]
  )
  legend(
    "topleft",
    pch = 19,
    col = col,
    legend = 0:1,
    bty = "n"
  )
}
layout(1)

For d created in a post above I'm getting

image

EmilyLongGlasgow commented 3 months ago

Ok, thanks! I'll try to get this to work (right now it is throwing an error, but I'm sure that I'm just doing something wrong).