# Visualizing dm objects

#### 2025-07-02

Once you have all your primary keys set and all foreign key relations defined, a graphical representation of your data model offers a condensed view of the tables and the relationships between the tables. The following functions can be used to visualize the `dm` object:1

1. `dm_draw()`
2. `dm_set_colors()`
3. `dm_get_colors()`
4. `dm_get_available_colors()`

We use the prepared example `dm` object `dm_nycflights13(cycle = TRUE)`:

    library(dm)
    library(dplyr)
    flights_dm_w_many_keys <- dm_nycflights13(cycle = TRUE, color = FALSE)
    flights_dm_w_many_keys

The schema is drawn with `dm_draw()`.

    dm_draw(flights_dm_w_many_keys)

You can use colors to visually group your tables into families to reflect their logical grouping. The available colors are either hexcoded colors or the standard R color names. The function `dm_get_available_colors()` forwards to `grDevices::colors()`:

    dm_get_available_colors()

Colors are assigned with `dm_set_colors()` using syntax known in the {tidyverse} as {tidyselect}-syntax, here in the form: `color = table`. Select helper functions are supported. The result of `dm_set_colors()` is a `dm` object. The information about the color is stored together with the rest of the metadata.

    flights_dm_w_many_keys_and_colors <-
      flights_dm_w_many_keys %>%
      dm_set_colors(
        maroon4 = flights,
        orange = starts_with("air"),
        "#5986C4" = planes
      )

Draw the schema with `dm_draw()`.

    dm_draw(flights_dm_w_many_keys_and_colors)

The colors can be queried with `dm_get_colors()`.

    dm_get_colors(flights_dm_w_many_keys_and_colors)

See the documentation for `dm_draw()` for further options. One important argument is `view_type`. Besides the default `"keys_only"`, it accepts `"all"` to display all columns, and `"title_only"` to show only the title of the table.

    flights_dm_w_many_keys_and_colors %>%
      dm_draw(view_type = "title_only")

If you would like to visualize only some of the tables, use `dm_select_tbl()` before drawing:

    flights_dm_w_many_keys_and_colors %>%
      dm_select_tbl(flights, airports, planes) %>%
      dm_draw()

Finally, for exporting a drawing to `svg` you could use `DiagrammeRsvg::export_svg()`:

    flights_dm_w_many_keys_and_colors %>%
      dm_select_tbl(flights, airports, planes) %>%
      dm_draw() %>%
      DiagrammeRsvg::export_svg() %>%
      write("flights_dm_w_many_keys_and_color.svg")

1. The code for the functions in this section is borrowed from the {datamodelr} package.↩︎