[R] [External] Package for "design graphs"

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Aug 19 08:21:18 CEST 2021


Hello,

Here is a igraph function to plot the graph as bipartite graph (which it 
is).
The reordering of the input dataframe columns, the order() call and 
vertex label distance value are a hack, and the plot could use the dots 
argument to allow the user to choose other custom graphics elements.
I post it mostly to show a graph theoretical solution is also possible.



library(igraph)

ig_DesignGraph <- function(x, vertex.size = 10, vertex.color = "black", 
edge.color = "gray"){
   g <- graph_from_data_frame(x[2:1], directed = FALSE)
   V(g)$type <- V(g)$name %in% x[[2]]
   V(g)$color <- V(g)$type
   V(g)$color <- gsub("FALSE", vertex.color, V(g)$color)
   V(g)$color <- gsub("TRUE", vertex.color, V(g)$color)
   L0 <- layout_as_bipartite(g)[, 2:1]
   i <- order(L0[, 1], L0[, 2])
   plot(
     g,
     edge.color = edge.color,
     vertex.size = vertex.size,
     vertex.label.dist = -3,
     vertex.label.degree = pi*V(g)$type,
     layout = -L0[i, ]
   )
}

ig_DesignGraph(expt1)
ig_DesignGraph(expt2)


Hope this helps,

Rui Barradas


Às 01:22 de 19/08/21, Richard M. Heiberger escreveu:
> Thank you for the example.
> 
> Here is a simple function in base graphics that does what you ask for.
> 
> You can turn off the default borders and ticks and tick labels and xlab and ylab,
> and add your own x tick labels, and then it will look exactly like the example you sent.
> 
> Rich
> 
> 
> expt1 <- data.frame(from=c(1,1,2,2,3,3,4,4),
>                      to=c("A","B","A","B","C","D","C","D"))
> 
> expt2 <-  data.frame(from=c(1,1,2,2,3,3,4,4),
>                       to=c("A","B","B","C","C","D","D","A"))
> 
> 
> DesignGraph <- function(x, pch.from=19, pch.to=19) {
>    from <- unique(x$from)
>    to <- unique(x$to)
> 
> 
>    n.from <- length(from)
>    n.to <- length(to)
>    Nrows<- max(1:n.from, 1:n.to)
> 
>    plot(1 ~ 1, type="n", xlim=c(1-.5, 2+.5), ylim=c(Nrows, 1))
> 
>    points(x=rep(1, n.from), y=1:n.from, pch=pch.from)
>    text(x=1-.3, y=1:n.from, labels=from)
> 
>    points(x=rep(2, n.to), y=1:n.to, pch=pch.to)
>    text(x=2+.3, y=1:n.to, labels=to)
> 
>    index.from <- which
>    index.to <- which
> 
>    segments(1, match(x$from, from), 2, match(x$to, to))
> }
> 
> DesignGraph(expt1)
> 
> DesignGraph(expt2)
> 
> 
> 
> 
> 
> 
>> On Aug 18, 2021, at 10:29, madsmh using gmail.com wrote:
>>
>> I have attached a photo from our book
>>
>> E. Hansen "Introduktion til matematisk statistik"
>>
>> the numbers represent the labels of one factor while the letters
>> represent the labels of anothr factor.
>>
>> .. Mads
>>
>>
>> On Tue, 2021-08-17 at 22:42 +0000, Richard M. Heiberger wrote:
>>> can you post an example of the graph?
>>>
>>> From: R-help <r-help-bounces using r-project.org> on behalf of
>>> madsmh using gmail.com <madsmh using gmail.com>
>>> Sent: Tuesday, August 17, 2021 16:02
>>> To: r-help using r-project.org
>>> Subject: [External] [R] Package for "design graphs"
>>>
>>> Hi,
>>>
>>> in our course littrature a "design graph" of two factors R and S with
>>> associated maps s : I -> S and f : I -> S where I is some finite
>>> index
>>> set, is a graph with factor labeles as vertices and lines f(i) to
>>> s(i)
>>> for all observations i in I. Is there a package on CRAN that can draw
>>> graphs like this automatically?
>>>
>>> I haven't been able to find anyting by searching.
>>>
>>> Regards, Mads
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Crmh%40temple.edu%7Cf18619e1691a4333682a08d962549a74%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637648937938659341%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=0LTywXuQ5Y%2FymuUTKOQeeozEx4MpAnF9QavJBcd4FNE%3D&reserved=0
>>> PLEASE do read the posting guide
>>> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Crmh%40temple.edu%7Cf18619e1691a4333682a08d962549a74%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637648937938659341%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=GT1zdL3%2BOAuVkXGGRMysbsfucmiIz6Dqozr6xyNbm8s%3D&reserved=0
>>> and provide commented, minimal, self-contained, reproducible code.
>> <IMG_0665.JPG>
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list