Skip to content

Commit

Permalink
Adjusted documentation, examples, and cookbook to match new colors an…
Browse files Browse the repository at this point in the history
…d palettes
  • Loading branch information
sambtalcott committed Oct 31, 2023
1 parent abaa76a commit 0026ac2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
17 changes: 9 additions & 8 deletions R/tntp_colors.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
#' #... multiple colors ...
#' ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
#' geom_point() +
#' scale_color_manual(values = tntp_colors('green', 'orange', 'red'))
#' scale_color_manual(values = tntp_colors('green', 'navy', 'red'))
#'
#' #... or a list of all possible TNTP brand colors
#' tntp_colors()
#'
#' # Use show_tntp_colors() to quickly see brand colors in the plotting window
#' show_tntp_colors('light_green', 'medium_green', 'green')
#' show_tntp_colors('mint', 'moss', 'green')
#'
#' # You can also use a pattern to return similar colors
#' show_tntp_colors(pattern = 'green')
Expand Down Expand Up @@ -257,26 +257,27 @@ tntp_palette_list <- list(
#' # Use to add a common palette to a ggplot visualization
#' ggplot(diamonds, aes(y = color, fill = cut)) +
#' geom_bar(position = "fill") +
#' scale_fill_manual(values = tntp_palette('likert_5', reverse = TRUE))
#' scale_fill_manual(values = tntp_palette('blues', reverse = TRUE))
#'
#'
#' # Use show_tntp_palette() to visualize a single or multiple palettes
#' show_tntp_palette('likert_7')
#' show_tntp_palette('bg_5', 'likert_5')
#'
#' # You can use a pattern to show similar palettes
#' show_tntp_palette(pattern = 'bg')
#' show_tntp_palette(pattern = 'top2')
#' show_tntp_palette(pattern = '_6')
#'
#' # Or run it with no specified palettes to see all available palettes
#' show_tntp_palette()
#'
#' # For creating a continuous color palette, use scale_color_gradient()
#' # along with tntp_colors():
#' ggplot(diamonds, aes(depth, table, color = price)) +
#' geom_point() +
#' scale_color_gradient(low = tntp_colors('light_green'),
#' ggplot(mtcars, aes(hp, disp, color = mpg)) +
#' geom_point(size = 3) +
#' scale_color_gradient(low = tntp_colors('red'),
#' high = tntp_colors('green'))
#'


tntp_palette <- function(palette = "likert_6", reverse = FALSE) {

Expand Down
4 changes: 2 additions & 2 deletions R/tntp_style.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
#' frpl_experience |>
#' ggplot(aes(frpl_bucket, avg_experience)) +
#' geom_col(fill = if_else(frpl_experience$frpl_bucket == "60-80%",
#' palette_tntp("orange"),
#' palette_tntp("medium_gray")
#' tntp_colors("tangerine"),
#' tntp_colors("medium_gray")
#' )) +
#' geom_text(aes(label = label),
#' nudge_y = -0.25, vjust = 1,
Expand Down
4 changes: 2 additions & 2 deletions man/tntp_colors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/tntp_palette.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/tntp_style.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions vignettes/visualization-cookbook.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ plt +
tntp_style()
```

### Setting font and font sizes
### Setting font family and font sizes

We will highlight two parameters to `tntp_style()` that can help customize your plot.

*`font`*
*`family`*

The `font` parameter lets you set the text font family. This changes the font family for all items, including plot and axis titles, legend titles and text, and axis text. The parameter defaults to 'Segoe UI', TNTP's font style, but uses 'sans' if 'Segoe UI' is not available. You will receive a warning if your specified font is not found.
The `family` parameter lets you set the text font family. This changes the font family for all items, including plot and axis titles, legend titles and text, and axis text. The parameter defaults to 'Halyard Display', TNTP's font style, but uses 'sans' if 'Halyard Display' is not available. You will receive a warning if your specified font is not found. You can also specify a separate header font using the `header_family` parameter.

You will need to manually import additional fonts using the `extrafont` package before they are available to R. Follow these steps to import fonts:

Expand Down Expand Up @@ -115,7 +115,7 @@ Below are the font sizes when `base_size` is set to the default of 28:

```{r}
plt +
tntp_style(show_axis_titles = TRUE, font = 'sans', base_size = 20) +
tntp_style(show_axis_titles = TRUE, family = 'sans', base_size = 20) +
theme(plot.subtitle = ggplot2::element_text(family = 'serif', size = 25))
```

Expand All @@ -133,7 +133,7 @@ To see all available settings you can adjust with a theme element, see the docum
The `tntpr` package includes functions to allow you to easily use TNTP brand colors in your visualizations. The primary function you'll use is `tntp_colors()`, which takes in human-friendly color names as arguments and returns a character vector of the equivalent hex code:

```{r}
tntp_colors("green", "light_green")
tntp_colors("green", "mint")
```

If you want to see all possible color names, you can run the function with no arguments:
Expand Down Expand Up @@ -202,7 +202,7 @@ ggplot(avg_salary, aes(x = school_year, y = avg_salary)) +

## Make a multiple line chart

Note that in the multiple line chart, we overwrote `ggplot2`'s default color scheme to incorporate TNTP colors. We pulled the hex codes for the colors we wanted to use with `tntp_colors()` and added them to the chart with `scale_colour_manual()`.
Note that in the multiple line chart, we overwrote `ggplot2`'s default color scheme to incorporate TNTP colors. We pulled the hex codes for the colors we wanted to use with `tntp_palette()` and added them to the chart with `scale_colour_manual()`.

```{r}
#Prepare data
Expand All @@ -216,13 +216,13 @@ school_names <- unique(school_salary$school_name)
# only plot two schools
line_plot_schools <- school_salary |>
filter(school_name %in% school_names[1:2])
filter(school_name %in% school_names[1:3])
```

```{r}
ggplot(line_plot_schools, aes(x = school_year, y = avg_salary, color = school_name)) +
geom_line(size = 1) +
geom_line(linewidth = 1) +
scale_y_continuous(labels = scales::dollar, limits = c(0, 5000)) +
scale_colour_manual(values = tntp_palette("colorful")) +
labs(
Expand All @@ -247,7 +247,7 @@ bar_df <- school_salary |>
ggplot(bar_df, aes(x = school_name, y = avg_salary)) +
geom_bar(stat="identity",
position="identity",
fill= tntp_colors('yellow')) +
fill= tntp_colors('gold')) +
scale_y_continuous(labels = scales::dollar, limits = c(0, 5000)) +
labs(
title="Acacia had higher average salaries in 2015",
Expand Down Expand Up @@ -353,7 +353,7 @@ scores_salary <- county_data |>

```{r}
ggplot(scores_salary, aes(avg_salary, perc_rank_test_score)) +
geom_point(color = tntp_colors('orange'), alpha = 0.9, size = 3) +
geom_point(color = tntp_colors('tangerine'), alpha = 0.9, size = 3) +
labs(
title = 'Schools with higher salaries do not have higher test scores',
subtitle = 'Relationship between school test scores and salaries',
Expand All @@ -379,8 +379,8 @@ ggplot(dumbbell_df, aes(x = `under $2.5k`, xend = `$4.5k+`, y = fct_reorder(scho
geom_dumbbell(
color = tntp_colors("light_grey"),
size = 3,
colour_x = tntp_colors('dark_yellow'),
colour_xend = tntp_colors('dark_green'),
colour_x = tntp_colors('gold'),
colour_xend = tntp_colors('green'),
show.legend = TRUE
) +
labs(title = "More teachers earn over $4.5k than under $2.5k",
Expand All @@ -397,7 +397,7 @@ number_teachers_school <- county_data |>
count(school_year, school_name)
ggplot(number_teachers_school, aes(n)) +
geom_histogram(binwidth = 5, colour = "white", fill = tntp_colors('blue')) +
geom_histogram(binwidth = 5, colour = "white", fill = tntp_colors('navy')) +
labs(
title = "Schools have a wide distribution in the number of teachers",
subtitle = "Total number of teachers per school",
Expand Down Expand Up @@ -476,17 +476,17 @@ teacher_survey_he_perc <- teacher_survey_he_perc |>

## Plot likert items - highlights top two

The first plot calls out the two highest response values, 'Strongly Agree' and 'Agree'. Note that the `tntp_style()` function does not alter the font size of the text within `geom_text()` so we set it manually.
The first plot calls out the two highest response values, 'Strongly Agree' and 'Agree'. Note that the `tntp_style()` function does not alter the font size or family of the text within `geom_text()` so we set it manually.

```{r}
ggplot(teacher_survey_he_perc, aes(percent, question, fill = response)) +
geom_col() +
geom_text(
aes(label = scales::percent(strong_response_percent, accuracy = 1), x = strong_response_percent),
color = 'white', fontface='bold', size = 5, hjust = 1.05
color = 'white', fontface='bold', family = "Halyard Display", size = 5, hjust = 1.05
) +
scale_x_continuous(labels = scales::percent, limits = c(0,1)) +
scale_fill_manual(values = rev(tntp_palette("likert_6")), drop = FALSE) +
scale_fill_manual(values = rev(tntp_palette("top2_6", reverse = TRUE)), drop = FALSE) +
guides(fill=guide_legend(nrow=2, byrow=TRUE, reverse = TRUE)) +
labs(title = "High Expectations Survey Responses") +
tntp_style(base_size = base_font_size)
Expand All @@ -506,7 +506,8 @@ teacher_survey_div <- teacher_survey_he_perc |>
ggplot(teacher_survey_div, aes(x = perc_diverge, y = question, fill = response)) +
geom_col() +
scale_fill_manual(
values = rev(tntp_palette("likert_6")), drop = FALSE,
values = tntp_palette("likert_6"),
drop = FALSE,
breaks = agree_disagree_scale,
labels = agree_disagree_scale
) +
Expand Down

0 comments on commit 0026ac2

Please sign in to comment.