Skip to content

Adding font and offset arguments #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Imports:
License: GPL-2
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
34 changes: 26 additions & 8 deletions R/bbc_style.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
#' Add bbc theme to ggplot chart
#'
#' This function allows you to add the bbc theme to your ggplotgraphics.
#' @param font Font with which the plot is made. By default, the value is \emph{Helvetica} but Windows machines do not support this font type.
#' @param offset Parameter that allows adding or substracting the \code{offset} value to the original font size. In particular, it modifies the size of:
#' \itemize{
#' \item The chart's title
#' \item The chart's subtitle
#' \item The legend
#' \item The axis
#' \item Title of the facet-wrap
#' }
#' @keywords bbc_style
#' @export
#' @examples
#' \dontrun{
#' line <- ggplot(line_df, aes(x = year, y = lifeExp)) +
#' geom_line(colour = "#007f7f", size = 1) +
#' geom_hline(yintercept = 0, size = 1, colour="#333333") +
#' bbc_style()
#' }
#'
#'\dontrun{
#' # Font changed to Arial and reduced by 5 points
#' line <- ggplot(line_df, aes(x = year, y = lifeExp)) +
#' geom_line(colour = "#007f7f", size = 1) +
#' geom_hline(yintercept = 0, size = 1, colour="#333333") +
#' bbc_style(font = "Arial", offset = -5)
#' }

bbc_style <- function() {
font <- "Helvetica"
bbc_style <- function(font = "Helvetica", offset = 0) {

ggplot2::theme(

#Text format:
#This sets the font, size, type and colour of text for the chart's title
plot.title = ggplot2::element_text(family=font,
size=28,
size=28 + offset,
face="bold",
color="#222222"),
#This sets the font, size, type and colour of text for the chart's subtitle, as well as setting a margin between the title and the subtitle
plot.subtitle = ggplot2::element_text(family=font,
size=22,
size=22 + offset,
margin=ggplot2::margin(9,0,9,0)),
plot.caption = ggplot2::element_blank(),
#This leaves the caption text element empty, because it is set elsewhere in the finalise plot function
Expand All @@ -35,14 +53,14 @@ bbc_style <- function() {
legend.title = ggplot2::element_blank(),
legend.key = ggplot2::element_blank(),
legend.text = ggplot2::element_text(family=font,
size=18,
size=18 + offset,
color="#222222"),

#Axis format
#This sets the text font, size and colour for the axis test, as well as setting the margins and removes lines and ticks. In some cases, axis lines and axis ticks are things we would want to have in the chart - the cookbook shows examples of how to do so.
axis.title = ggplot2::element_blank(),
axis.text = ggplot2::element_text(family=font,
size=18,
size=18 + offset,
color="#222222"),
axis.text.x = ggplot2::element_text(margin=ggplot2::margin(5, b = 10)),
axis.ticks = ggplot2::element_blank(),
Expand All @@ -58,8 +76,8 @@ bbc_style <- function() {
#This sets the panel background as blank, removing the standard grey ggplot background colour from the plot
panel.background = ggplot2::element_blank(),

#Strip background (#This sets the panel background for facet-wrapped plots to white, removing the standard grey ggplot background colour and sets the title size of the facet-wrap title to font size 22)
#Strip background (#This sets the panel background for facet-wrapped plots to white, removing the standard grey ggplot background colour and sets the title size of the facet-wrap title to font size 22 + offset)
strip.background = ggplot2::element_rect(fill="white"),
strip.text = ggplot2::element_text(size = 22, hjust = 0)
strip.text = ggplot2::element_text(size = 22 + offset, hjust = 0)
)
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ A basic explanation and summary here:

### `bbc_style()`

1. `bbc_style()`: has no arguments and is added to the ggplot chain after you have created a plot. What it does is generally makes text size, font and colour, axis lines, axis text and many other standard chart components into BBC style, which has been formulated together with the Visual Journalism design team.
1. `bbc_style()`: has two arguments and is added to the ggplot chain after you have created a plot. What it does is generally makes text size, font and colour, axis lines, axis text and many other standard chart components into BBC style, which has been formulated together with the Visual Journalism design team. The arguments allow to adjust the the font and the size of it.

The function is pretty basic and does not change or adapt based on the type of chart you are making, so in some cases you will need to make additional `theme` arguments in your ggplot chain if you want to make any additions or changes to the style, for example to add or remove gridlines etc. Also note that colours for lines in the case of a line chart or bars for a bar chart, do not come out of the box from the `bbc_style` function, but need to be explicitly set in your other standard `ggplot` chart functions.

Expand Down
24 changes: 23 additions & 1 deletion man/bbc_style.Rd

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

11 changes: 8 additions & 3 deletions man/finalise_plot.Rd

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