From 0026ac231aebe3bbcd40faa59371d238a441ae9d Mon Sep 17 00:00:00 2001 From: Sam Talcott Date: Tue, 31 Oct 2023 12:44:34 -0400 Subject: [PATCH] Adjusted documentation, examples, and cookbook to match new colors and palettes --- R/tntp_colors.R | 17 +++++++------- R/tntp_style.R | 4 ++-- man/tntp_colors.Rd | 4 ++-- man/tntp_palette.Rd | 12 +++++----- man/tntp_style.Rd | 4 ++-- vignettes/visualization-cookbook.Rmd | 35 ++++++++++++++-------------- 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/R/tntp_colors.R b/R/tntp_colors.R index cd0db34..fe9bb50 100644 --- a/R/tntp_colors.R +++ b/R/tntp_colors.R @@ -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') @@ -257,7 +257,7 @@ 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 @@ -265,18 +265,19 @@ tntp_palette_list <- list( #' 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) { diff --git a/R/tntp_style.R b/R/tntp_style.R index 5a6cff0..7ead506 100644 --- a/R/tntp_style.R +++ b/R/tntp_style.R @@ -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, diff --git a/man/tntp_colors.Rd b/man/tntp_colors.Rd index f0065ca..9ccc6b7 100644 --- a/man/tntp_colors.Rd +++ b/man/tntp_colors.Rd @@ -47,13 +47,13 @@ ggplot(mtcars, aes(wt, mpg)) + #... 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') diff --git a/man/tntp_palette.Rd b/man/tntp_palette.Rd index ede46db..7374fd0 100644 --- a/man/tntp_palette.Rd +++ b/man/tntp_palette.Rd @@ -28,7 +28,7 @@ library(ggplot2) # 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 @@ -36,16 +36,16 @@ 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')) - } diff --git a/man/tntp_style.Rd b/man/tntp_style.Rd index b939329..2df52c1 100644 --- a/man/tntp_style.Rd +++ b/man/tntp_style.Rd @@ -77,8 +77,8 @@ frpl_experience <- fake_county |> 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, diff --git a/vignettes/visualization-cookbook.Rmd b/vignettes/visualization-cookbook.Rmd index ff2a7cb..df79733 100644 --- a/vignettes/visualization-cookbook.Rmd +++ b/vignettes/visualization-cookbook.Rmd @@ -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: @@ -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)) ``` @@ -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: @@ -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 @@ -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( @@ -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", @@ -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', @@ -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", @@ -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", @@ -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) @@ -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 ) +