Pie Charts in RCy3

How to represent nodes as pie charts using Cytoscape and RCy3

random-code-snippets
rcy3
cytoscape
R
Author

Robert M Flight

Published

January 6, 2022

I have a package, {categoryCompare2} (Flight 2022) that I’ve been working on for a while, and recently wanted to make available on our labs r-universe (“The ’Moseleybioinformaticslab’ Universe,” n.d.).

For some of the current visualization, we use Cytoscape to examine annotation similarity graphs, coupling the R together with Cytoscape via {RCy3} (Gustavsen et al. 2019). In the previous iteration, we had used actual piechart images generated by R, and then used setNodeImageDirect to point the node images to local image files.

However, the latest iteration of {RCy3} has essentially lost that functionality. However, there is a new visualization plugin that can do similar things, enhancedGraphics (Morris JH 2014).

Alexander Pico, one of the primary {RCy3} developers, provided me with the guidance for a code solution (Pico 2022), which I’ve adapted below.

grp_matrix = matrix(c(1, 0,
                      0, 1), nrow = 2, ncol = 2)
n_grp <- nrow(grp_matrix)
use_color <- rainbow_hcl(ncol(grp_matrix), c = 100)
n_color <- length(use_color)
use_color = color
# defines how many pie segments are needed, common to all the pie-charts
pie_area <- rep(1 / n_color, n_color)
names(pie_area) <- rep("", n_color)


desat_color <- desaturate(use_color)
names(desat_color) <- names(use_color)
piechart_strings <- purrr::map_dfr(rownames(grp_matrix), function(i_grp){
  tmp_logical <- grp_matrix[i_grp, ]
  tmp_color <- use_color
    
  # add the proper desaturated versions of the colors
  tmp_color[!tmp_logical] <- desat_color[!tmp_logical]
    
  out_str = paste0('piechart: attributelist="',
                   paste(colnames(grp_matrix), collapse = ','),
                   '" ',
                   'colorlist="',
                   paste(tmp_color, collapse = ','),
                   '" ',
                   'arcstart=-90 showlabels=false')
  data.frame(colorlist = out_str)
})

tmp_matrix = as.data.frame(matrix(1, nrow = nrow(piechart_strings),
                      ncol = ncol(grp_matrix)))
names(tmp_matrix) = colnames(grp_matrix) 
piechart_df = cbind(tmp_matrix, piechart_strings)

# after merging this with the node information to
# put the right things with the right node
# this gives **node_vis_df** below
RCy3::setNodeShapeDefault("ELLIPSE")
RCy3::setNodeSizeDefault(35)
RCy3::loadTableData(node_vis_df, data.key.column = "name", table = "node", table.key.column = "name")
RCy3::updateStyleMapping("default",
   RCy3::mapVisualProperty("NODE_CUSTOMGRAPHICS_1", "colorlist", "p"))

References

Flight, Robert M. 2022. “categoryCompare2.” https://github.com/MoseleyBioinformaticsLab/categoryCompare2.
Gustavsen, Julia A., Pai, Shraddha, Isserlin, Ruth, Demchak, Barry, Pico, and Alexander R. 2019. “RCy3: Network Biology Using Cytoscape from Within r.” F1000Research. https://doi.org/10.12688/f1000research.20887.3.
Morris JH, Ferrin TE, Kuchinsky A. 2014. “enhancedGraphics: A Cytoscape App for Enhanced Node Graphics.” F1000Research. https://doi.org/10.12688/f1000research.4460.1.
Pico, Alexander. 2022. “RCy3 GitHub Issue: Equivalent of setNodeImageDirect.” https://github.com/cytoscape/RCy3/issues/167#issuecomment-1004467137.
“The ’Moseleybioinformaticslab’ Universe.” n.d. https://moseleybioinformaticslab.r-universe.dev/ui#builds.

Reuse

Citation

BibTeX citation:
@online{mflight2022,
  author = {Robert M Flight},
  title = {Pie {Charts} in {RCy3}},
  date = {2022-01-06},
  url = {https://rmflight.github.io/posts/2022-01-06-pie-charts-in-rcy3},
  langid = {en}
}
For attribution, please cite this work as:
Robert M Flight. 2022. “Pie Charts in RCy3.” January 6, 2022. https://rmflight.github.io/posts/2022-01-06-pie-charts-in-rcy3.