# Sizing node labels to fit - large family tree

**URL:** <https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901>\
**Category:** Help\
**Created:** [November 27, 2023, 1:10am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901 "2023-11-27T01:10:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 27, 2023, 1:10am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/1 "2023-11-27T01:10:27Z")

</div>

I have been working on a family tree application (~ 5,000 nodes). I am working on a Mac (M2) in RStudio. I am using the RStudio to save the graph to a pdf file. Here’s the problem - long names run outside of the node box. I have fizedsize set to FALSE.

Any help appreciated. More than willing to provide additional information - I just don’t know how muc hto share.

Thanks,  
STerling

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [November 27, 2023, 1:43am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/2 "2023-11-27T01:43:01Z")

</div>

Please share a cut-down graph with a few examples of offending nodes that display “too long”

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 27, 2023, 4:39am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/3 "2023-11-27T04:39:28Z")

</div>

Find attached a pdf file. The three nodes at the bottom all exceed the box width in the pdf.

[Rplot03.pdf](https://forum.graphviz.org/uploads/short-url/5Ax7r3fCd2AbZujE0oowE1WXdTH.pdf) (164.3 KB)

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [November 27, 2023, 4:54am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/4 "2023-11-27T04:54:00Z")

</div>

What we need is a (smaller) input file (the graph definition).

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 27, 2023, 6:32am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/5 "2023-11-27T06:32:35Z")

</div>

Hmmm. The input file was. GED file I processed. Here’s the code related to actually plotting  
Currently font size is set to 30. The pdf is currently set to 30" x 20". I’ve struggled to related the node width and height data (which I believe is in inches), with the font size (measured in pts). I’ve made the font smaller than 30, but then the text in the pdf disappears.

I’m not sure how pdfs work. It would appear the text is somehow scaled when fitting the graph to the page. If so, then with the page ~ 100 nodes wide, that’s 1/3 inch per node or 24 pts. If 24 characters are fit across a node, then each character is rendered at 1 pt font. But I’m not sure if that’s how things work.

setMethod(“plot”  
,signature(gt=“GenTree”  
#,format=“list”  
)  
,function(gt) { #},format) {

```
          # get the node data frame, with columns 
          # id: id of node
          # label: label to be displayed (includes line breaks)
          # colour: colour of node (gender based)
          # shape: shape of node
          # fontSize: font size to be used for node (30)
          nodes <- gt@nodes
          
          # get the edge data frame, with columns
          # from: the node at which edge starts
          # to: the node at which the edge ends
          # colour: the colour of the edge (grey)
          # width: the width of the edge (0.1)
          edges <- gt@edges

          g <- graphNEL(nodes=nodes$id,edgemode="directed")

          g <- addEdges(g,gt@edges)
          g1 <- layoutGraph(g)

          nodeColour <- nodes$colour
          names(nodeColour) <- nodes$id
          nodeRenderInfo(g1) <- list(fill=nodeColour)
          
          nodeLineColour <- nodes$colour
          names(nodeLineColour) <- nodes$id
          nodeRenderInfo(g1) <- list(col=nodeLineColour)
          
          nodeShape <- nodes$shape
          names(nodeShape) <- nodes$id
          nodeRenderInfo(g1) <- list(shape=nodeShape)
          
          labels <- nodes$label
          names(labels) <- nodes$id
          
          nodeFontSize <- nodes$fontSize
          # graph.par sets parameters for all items. In this case, set
          # the font size for all nodes
          graph.par(list(nodes=list(fontsize=nodes$fontSize)))
          names(nodeFontSize) <- nodes$id
          
          nodeRenderInfo(g1) <- list(label=labels)
          
          fixedSize <- rep(FALSE,length(nodeLineColour))
          names(fixedSize) <- nodes$id
          nodeRenderInfo(g1) <- list(fixedsize=fixedSize)
         
          edgeColour <- gt@edges$colour
          # need to sort the edge information in the same order as edge names
          eNames <- paste0(gt@edges$from,"~",gt@edges$to)
          edgeColour <- edgeColour[match(edgeNames(g1),eNames)]
          names(edgeColour) <- edgeNames(g1)
          edgeRenderInfo(g1) <- list(col=edgeColour)
            
          
          graphRenderInfo(g1,c(nodesep=.5,width=60))
          renderGraph(g1)
          gt@graph <- g1
          
        return(gt)
          

}#function(gt,format)

```

)

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [November 28, 2023, 12:57am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/6 "2023-11-28T00:57:22Z")

</div>

I can’t debug **RStudio** or **R** (I assume **RStudio** uses **R** , but maybe not).  
If you can create an output file of type **dot** instead of **pdf** , I will try to help.  
Otherwise, try asking the **RStudio** folks.

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 28, 2023, 3:11pm UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/7 "2023-11-28T15:11:43Z")

</div>

Ok. I will look into generating a dot file for you

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 29, 2023, 1:06am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/8 "2023-11-29T01:06:55Z")

</div>

Hi Steve,

Find attached the dot file. I had created a graphNEL object, so had to convert it using agopenSimple, and then used toFile to create the dot file.  
[fred.dot](https://forum.graphviz.org/uploads/short-url/22BfNBmdu2FOTTxXjwgqQ31sLk1.dot) (101.3 KB)

---

<div class="post-metadata">

**Author:** ![scnorth](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/scnorth/32/89_2.png) [@scnorth](https://forum.graphviz.org/u/scnorth)\
**Post date:** [November 29, 2023, 3:40am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/9 "2023-11-29T03:40:23Z")

</div>

It’s not a problem with Graphviz or with your graph. It’s apparently a mismatch between the fonts Graphviz found and loaded when it computed the layout, and the fonts used in the final renderer in RStudio. It’s similar to a problem people sometimes have with graphviz SVG output, where you can’t be certain how a given font specification will be resolved at render time on different machines or even by different programs on the same machine. For now, probably the easiest thing is for you to just draw the graph using a shell command, e.g. `dot -Gsize="10,10" -Tpdf fred.dot -o fred.pdf` unless anyone has a better idea.

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 29, 2023, 4:20pm UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/10 "2023-11-29T16:20:47Z")

</div>

Many thanks. So … I’m guessing there’s a Mac executable for dot somewhere nearby. 🙂

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 29, 2023, 4:49pm UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/11 "2023-11-29T16:49:40Z")

</div>

Ok. So I tried the suggested solution, and I now have a graph, with node width adjusted to fit the text. Nodes contain the id text, but node colour and labels are not presented. I had used ‘toFile’ to generate the dot file, so I would expect the dot file to have contained the needed information? Is there more to the command line that I simply need to learn about?

Thanks again for your patience

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [November 29, 2023, 6:06pm UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/12 "2023-11-29T18:06:41Z")

</div>

All the style, color, shape, … was lost in transit. If you look at _fred.dot_, you won’t find any of it.  
Possibilities:

- Find a way to get RStudio/R to provide all the stylish information
- explicitly increase the **width** attribute of the “too small” nodes or all the nodes (I am skeptical)
- insert _more_ newlines (“\n”) into long names to make the text line shorter (then make the name text a different color to make it easier to read)
- try different fonts (e.g. see [Font FAQ | Graphviz](https://graphviz.org/faq/font/#default-fonts-and-postscript-fonts)) to see if you can find one that works OK

---

<div class="post-metadata">

**Author:** ![sterling](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sterling](https://forum.graphviz.org/u/sterling)\
**Post date:** [November 30, 2023, 1:26am UTC](https://forum.graphviz.org/t/sizing-node-labels-to-fit-large-family-tree/1901/13 "2023-11-30T01:26:18Z")

</div>

This is likely a RGraphviz problem, rather than Graphviz. The graph on the monitor is rendered properly, while the exported graph (to dot file) is not.

Thanks for all the help getting me this far.  
Sterling
