rchlumsk / RavenR

R package for handling Raven hydrologic modelling framework inputs, outputs, and diagnostics
36 stars 16 forks source link

update rvn_rvi_process_plot to avoid overlapping labels #76

Closed rchlumsk closed 3 years ago

rchlumsk commented 3 years ago

Ideal updates to this feature include:

rchlumsk commented 3 years ago

I have been playing around with the Diagrammer package https://github.com/rich-iannone/DiagrammeR, I think this may be a good way to provide functionality for handling these types of aesthetics. The function would then return a diagrammer object (similar to igraph object) rather than a ggplot, with additional option to export the figure to pdf or png for example.

Still working on some of the aesthetics, placement, and conditionality, but here is a figure generated from it so far, and code to produce it. In theory the code can be simplified, but the conversion from igraph or data frame to diagrammer plot directly seems to have some kind of bugs, so opted to build the plots manually in a loop. We may wish to avoid pulling in Diagrammer as a dependency, but based on its usage of the igraph library (which is already a dependency), this may not add that much overhead.

    library(RavenR)
    library(DiagrammeR)

    # these two are also need to be installed in order to produce plots at all, apparently
    library(DiagrammeRsvg)
    library(rsvg)

    # RavenR reading in of rvi file
    rvi <- rvn_rvi_read(system.file("extdata","Nith.rvi", package="RavenR"))
    connections <- rvn_rvi_connections(rvi)

    # fix connections to all be non-conditional, except last one for testing
    connections$Conditional <- FALSE
    connections$Conditional[20] <- TRUE

    # build vertices from connections data
    verts <- unique( c(connections$From,connections$To) )

    # build diagrammer graph and start building, add verticies, then add labels
    dg <- create_graph()

    for (i in 1:length(verts)) {
      dg <- dg %>% add_node(label=verts[i])
    }

    for (i in 1:nrow(connections)) {

      if (connections$Conditional[i]) {
        dg <- dg %>% add_edge(from=connections$From[i],
                              to=connections$To[i],
                              edge_aes = edge_aes(
                                color = "red"
                                ),
                              edge_data=list("algorithm"=connections$Algorithm[i],
                                             "processtype"=connections$ProcessType[i],
                                             "conditional"=connections$Conditional[i]))
      } else {
        dg <- dg %>% add_edge(from=connections$From[i],
                              to=connections$To[i],
                              edge_data=list("algorithm"=connections$Algorithm[i],
                                             "processtype"=connections$ProcessType[i],
                                             "conditional"=connections$Conditional[i]))
      }
    }

    # render graph in viewer
    render_graph(dg)

dg

rchlumsk commented 3 years ago

Updated with rvn_rvi_process_diagrammer function - closing this issue as it resolves the overlapping labels, although future updates to accomodate additional state variables and aesthetic changes may be desired.

Propose that the rvn_rvi_process_plot be kept as parallel funcitonality with ggplot, and perhaps just renamed to rvn_rvi_process_ggplot for clarity