Giter Site home page Giter Site logo

bbplot's Introduction

BBPLOT

This repo contains the functions of the bbplot package, which once installed locally, provides helpful functions for creating and exporting graphics made in ggplot in the style used by the BBC News data team.

Example of graphics created using the bbplot package

Installing bbplot

bbplot is not on CRAN, so you will have to install it directly from Github using devtools.

If you do not have the devtools package installed, you will have to run the first line in the code below as well.

# install.packages('devtools')
devtools::install_github('bbc/bbplot')

Using the functions

The package has two functions for plots: bbc_style() and finalise_plot.

Detailed examples on how to use the functions included within the bbplot package to produce graphics are included in the R cookbook, as well as a more general reference manual for working with ggplot2.

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.

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.

Example of how it is used in a standard workflow:

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()

finalise_plot

  1. finalise_plot: will save out your plot with the correct guidelines for publication for a BBC News graphic. It is made up of functions that will left align your title, subtitle and source, add the BBC blocks at the bottom right and save it to your specified location. The function has six arguments, three of which need to be explicitly set and three that are defaults unless you overwrite them.

Here are the function arguments: finalise_plot(plot_name, source_name, save_filepath, width_pixels, height_pixels, logo_image_path)

  • plot_name: the variable name that you have called your plot, for example for the chart example above plot_name would be "line"
  • source_name: the source text that you want to appear at the bottom left corner of your plot. You will need to type the word "Source:" before it, just the source, so for example source = "Source: ONS" would be the right way to do that.
  • save_filepath: the precise filepath that you want your graphic to save to, including the .png extension at the end. This does depend on your working directory and if you are in a specific R project. An example of a relative filepath would be: /charts/line_chart.png.
  • width_pixels: this is set to 640px by default, so only call this argument and specify the width you want your chart to be.
  • height_pixels: this is set to 450px by default, so only call this argument and specify the height you want your chart to be.
  • logo_image_path: this argument specifies the path for the image/logo in the bottom right corner of the plot. The default is for a placeholder PNG file with a background that matches the background colour of the plot, so do not specify the argument if you want it to appear without a logo. If you want to add your own logo, just specify the path to your PNG file. The package has been prepared with a wide and thin image in mind.

Example of how the finalise_plot() is used in a standard workflow. This function is called once you have created and finalised your chart data, titles and added the bbc_style() to it (see above):

finalise_plot(plot_name = my_line_plot,
source = "Source: ONS",
save_filepath = "filename_that_my_plot_should_be_saved_to-nc.png",
width_pixels = 640,
height_pixels = 550)

bbplot's People

Contributors

cguibourg avatar nassosstylianou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bbplot's Issues

Error Failed to install bbplot from GitHub

I'm trying to install bbplot and received the following error messages:

Error: Failed to install 'bbplot' from GitHub:
(converted from warning) unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.5:
cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.5/PACKAGES'

The code that I used was:
install.packages('devtools')
devtools::install_github('bbc/bbplot')

I'm using R 3.5.3 (2019-03-11) on a Windows 10 Enterprise machine. I get a code 404 error messages when I try to go to the URLs in the above error message.

Any suggestions would be great. Thanks.

line_df not defined in README.md

The dataframe used to plot the example, line_df is not defined.

Perhaps create an example df and save to the package, or provide code in .md to create line_df.

Windows Fonts

Windows does not have it's own Helvetica font, which results in warnings when using bbc_style() on a typical windows computer.

Running windowsFonts(Helvetica = "TT Arial") at the start of the script will use the windows default of treating Helvetica as Arial and avoid the warnings.

Logo_Image_Path not working as expected

Hi!

I've been using this package today to try it out and I am very happy with it.

I have a logo image that I want to add to my plot. I used the logo_image_path option in the finalise_plot function.
The logo is added to the plot, but it is not complete, it is cutted on the right side.

Is there a way to have it complete without having to change the dimensions of the image?

Thank you,
Carlotta.

Black bar at bottom of plot

Hi,

I've started using the bbplot package today which is generally working really well. However, when finalising the plot, the exports seem to include a black bar at the bottom of the image which masks the source details. I've fiddled with the code and it seems to be happening when the subtitle or source are longer than a certain number of characters (although still less wide than the width of the available space). I can't get any plots to finalise with both a subtitle and source at present.

Code:

svannualcount <- ggplot(svkc_count, aes(x = as.character(Recorded_Year), y = n)) +
  geom_col(fill="#082a52") +
  geom_hline(yintercept = 0, size = 1, colour="#333333") +
  theme(axis.text.x = element_text(angle = 0)) +
  bbc_style() +
  labs(title="This is a test title",
       subtitle = "Can you read my test title?")

finalise_plot(plot_name = svannualcount,
              source = "Source: This is a test source.",
              save_filepath = "/tmp/svkcannualplot.png",
              width_pixels = 640,
              height_pixels = 450,
              logo_image_path = "/tmp/vrulogo.png"
             )

Graphic:
image

image

[Feature request] - Add arguments to bbc_style() for easy customization

Windows machines do not support Helvetica font type, so I had to make modifications to the bbc_style() in a separate ggplot2() function. It would be cool to have maybe a font argument that defaults to Helvetica, but would allow others to change the font name. Once the font changes, there may be some sizing differences, so it would also be cool to create an offset size, to do a plus or minus the current font sizes used in your function: bbc_style <- function(font = "Helvetica", offset = 0), which would allow me to use it like this: bbc_style("Arial", -2).

Suggestion for "Color bars Conditionally" example in the cookbook.

Cool book. Just a suggestion here:

ggplot(bar_df,
aes(x = reorder(country, lifeExp), y = lifeExp, fill = country == "Mauritius")) +
geom_col() +
scale_fill_manual(values = c("#dddddd", "#1380A1"), guide = F) +
geom_hline(yintercept = 0, size = 1, colour="#333333") +
bbc_style() +
coord_flip() +
labs(title="Reunion is highest",
subtitle = "Highest African life expectancy, 2007") +
theme(panel.grid.major.x = element_line(color="#cbcbcb"),
panel.grid.major.y = element_blank())

Add a set of color palettes

I was reading your https://bbc.github.io/rcookbook/ and this is pretty great, but there's an easy step that you could take that would add simplicity and consistency to the plot making process: Take a look at RColorBrewer... it let's you very simply to call out colors as per a variety of themes... perhaps something like this could be done w/ BBC colours? This way one would never have to read a theme sheet, it would be built right in. Helpful?

image

Incongrous function name

The bbc_style function seems very similar to any other ggplot theme function, like the theme_grey, theme_classic, theme_light, etc., built in to the ggplot2 package, or many others like those in the ggthemes package.

Except the name.

I think it'd be great to change the name of the function to theme_bbc if you'd like greater visibility consider submitting it to ggthemes. With theme_economist, theme_fivethirtyeight, and theme_wsj, theme_bbc would feel right at home :)

The graphic can not show chinese

if I use the function bbc_style(), Chinese characters cannot show properly in the final graphic even when I add theme(text = element_text(family = "Kai")).
Delete bbc_style() can work properly.

So I think there may be some problem with bbc_style() when using Chinese characters.

Allow the use of logos from the internet

When finalizing a plot, it would be nice to be able to pull a logo from a URL. This would make it easier to share code examples without first requiring users to download the logos.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.