Genogram

library(DiagrammeR)

DiagrammeR::grViz("
digraph family_genogram {
  #Node attributes for all family members
  node [fontname=Times, style=filled, fillcolor=lightblue, shape=circle]
  Carlos [label='Carlos (Father)', shape=circle, fillcolor=lightblue]
  Maria [label='Maria (Mother)', shape=square, fillcolor=pink]
  Sofia [label='Sofia (Daughter)', shape=square, fillcolor=pink]
  Mateo [label='Mateo (Son)', shape=circle, fillcolor=lightblue]
  Grandma [label='Granny (Paternal)', shape=square, fillcolor=pink]
  Joan [label='Joan (Social Worker)' , shape=rectangle, fillcolor=limegreen]
  School [label='School', shape=rectangle, fillcolor=yellow]
  Work [label='Work', shape=rectangle,fillcolor=yellow]
  'Gender Roles' [label='Gender Roles', shape=rectangle, fillcolor=yellow]
  
  #Biological Relationships
  Grandma->Maria [label=<<font color='coral1'>Parenting Conflict</font>>, color=coral1]
  Grandma->Sofia [label=<<font color='darkslategray3'>Alliance</font>>, color=darkslategray3, dir=both]
  
  Maria->Carlos [label=<<font color='purple'>Poor Communication</font>>, dir=both, color=purple]
  
  Maria->Mateo [label=<<font color='violetred'>Absent</font>>, color=violetred, style=dashed]
  Maria->Sofia [label=<<font color='burlywood4'>Conflict</font>>, color=burlywood4, dir=both]
  Maria->Joan [label=<<font color='navy'>Seeks Help</font>>, color=navy]
  Maria->'Gender Roles' [label=<<font color='forestgreen'>Machismo/Marianismo</font>>, color=forestgreen, style='dashed']
  Maria->Grandma [label=<<font color='purple'>Poor Communication</font>>, dir=both, color=purple]
  
  Carlos->Grandma [label=<<font color='darkslateblue'>Defers Responsibility</font>>, color=darkslateblue, style=dashed]
  Carlos->Mateo [label=<<font color='violetred'>Absent</font>>, color=violetred, style=dashed]
  Carlos->Sofia [label=<<font color='violetred'>Absent</font>>, color=violetred, style=dashed]
  Carlos->Work [label=<<font color='forestgreen'>Machismo/Marianismo</font>>, color=forestgreen, style='dashed']
  Carlos->'Gender Roles' [label=<<font color='forestgreen'>Machismo/Marianismo</font>>, color=forestgreen, style='dashed']
  
  Sofia->Maria [label=<<font color='steelblue4'>Role Confusion</font>>, color=steelblue4]
  Sofia->Grandma [label=<<font color='steelblue4'>Role Confusion</font>>, color=steelblue4]
  Sofia->School [label=<<font color='red'>Disengaged</font>>, color=red, style=dashed]
  
  Mateo->Grandma [label=<<font color='steelblue4'>Role Confusion</font>>, color=steelblue4]
  Mateo->Maria [label=<<font color='steelblue4'>Role Confusion</font>>, color=steelblue4]
  

}
                  
                  ")

#Legend

library(DiagrammeR)

grViz("
digraph legend {
  graph [layout=dot, rankdir=TB]

  # Subgraph to create a vertical legend box
  subgraph cluster_legend {
    label = 'Legend';
    style = rounded;
    fontsize = 12;

    # Relationship types with colors and line styles
    Parenting_Conflict [label='Parenting Conflict', shape=plaintext, fontcolor=coral1, fontsize=10]
    Alliance [label='Alliance', shape=plaintext, fontcolor=darkslategray3, fontsize=10]
    Poor_Communication [label='Poor Communication', shape=plaintext, fontcolor=purple, fontsize=10]
    Absent [label='Absent', shape=plaintext, fontcolor=hotpink, fontsize=10]
    Conflict [label='Conflict', shape=plaintext, fontcolor=burlywood4, fontsize=10]
    Seeks_Help [label='Seeks Help', shape=plaintext, fontcolor=navy, fontsize=10]
    Machismo [label='Machismo/Marianismo', shape=plaintext, fontcolor=forestgreen, fontsize=10]
    Defers_Responsibility [label='Defers Responsibility', shape=plaintext, fontcolor=darkslateblue, fontsize=10]
    Role_Confusion [label='Role Confusion', shape=plaintext, fontcolor=steelblue4, fontsize=10]
    Disengaged [label='Disengaged', shape=plaintext, fontcolor=red, fontsize=10]

    # Invisible nodes for spacing
    L1 [shape=point, width=0, style=invis]
    L2 [shape=point, width=0, style=invis]
    L3 [shape=point, width=0, style=invis]
    L4 [shape=point, width=0, style=invis]
    L5 [shape=point, width=0, style=invis]
    L6 [shape=point, width=0, style=invis]
    L7 [shape=point, width=0, style=invis]
    L8 [shape=point, width=0, style=invis]
    L9 [shape=point, width=0, style=invis]
    L10 [shape=point, width=0, style=invis]

    # Each line now independently points to its label
    L1 -> Parenting_Conflict [color=coral1, style=solid]
    L2 -> Alliance [color=darkslategray3, style=solid]
    L3 -> Poor_Communication [color=purple, style=solid]
    L4 -> Absent [color=hotpink, style=dashed]
    L5 -> Conflict [color=burlywood4, style=solid]
    L6 -> Seeks_Help [color=navy, style=solid]
    L7 -> Machismo [color=forestgreen, style=dashed]
    L8 -> Defers_Responsibility [color=darkslateblue, style=dashed]
    L9 -> Role_Confusion [color=steelblue4, style=solid]
    L10 -> Disengaged [color=red, style=dashed]

    # Force proper vertical alignment for labels
    {rank=same; L1; Parenting_Conflict}
    {rank=same; L2; Alliance}
    {rank=same; L3; Poor_Communication}
    {rank=same; L4; Absent}
    {rank=same; L5; Conflict}
    {rank=same; L6; Seeks_Help}
    {rank=same; L7; Machismo}
    {rank=same; L8; Defers_Responsibility}
    {rank=same; L9; Role_Confusion}
    {rank=same; L10; Disengaged}
  }
}
")