Dr. Mark Gardener

Statistics for Ecologists Using R and Excel.

Edition 2.

Statistics for Ecologists Using R and Excel:
Data collection, exploration, analysis and presentation

Get a 20% discount on "Statistics for Ecologists" when you buy direct from the publisher! Enter the voucher code S4E20 in the shopping basket at Pelagic Publishing.

# Writer's Bloc

On this page you can find out about my latest writing project. I'll post updates on progress, tables of contents and also some of the R scripts (and possibly Excel spreadsheets) I am developing in support of the new book. I'll try to keep the material reasonably up to date.

The Writer's Bloc homepage contains a table of contents and an index of the pages that contain custom R commands and R scripts.

I am working on a new edition of my book Statistics for Ecologists Using R and Excel. I am currently revising the chapter(s) on graphics and thought I'd make some notes about colours on graphs and charts...

## Colouring in: Using colour in graphs and charts:

### Introduction

Colour is very important in presenting data and results. Both Excel and R have a wide range of colours you can use when creating your graphs and charts (certainly more than 50 shades of gray!).

Controlling and managing the colours you display is an importnat element in presenting your work. With an increasing volume of work being presented via the Internet, colour is something not to take for granted. Using default colours is "easy" but for maximum impact you should think carefully about how to present the best colours for the job.

Traditional journals generally use monochrome, which you can think of as just another set of colours, but even if you are "stuck" with shades of grey you need to think carefully. Pattern filling can be an especially useful option when using monochrome.

Set color in Excel:

Page Layout menu sets overall color themes.
Chart Tools > Design > Change Colors changes a chart color theme.
Select a chart element directly and format it.

Top

### Using colour in Excel charts

You can set colours in Excel in several ways:

• General color can be set from the Page Layout menu, which sets overall color themes.
• When you make a chart the Chart Tools > Design > Change Colors button allows you to "override" the general colors and set your own.
• You can format elements in charts directly from the Chart Tools > Format menu.
• Right-click or double-click a chart element directly.

When you set a colour explicitly you can also incorporate fill effects, with Pattern Fill being the most useful.

The Change Colors button (Chart Tools > Design menu) gives a limited range of color themes for Excel charts.

Top

### Quick settings in Excel charts

Whenever you make a chart in Excel it will have a default colour palette. You can alter the general colour theme for an existing chart from the Chart Tools menu. The exact option you select depends on the version of Excel you use e.g.

• 2013: Chart Tools > Design > Change Colors button.
• 2010: Chart Tools > Design > Chart Styles section.

This gives you a few options for altering the general flavour of the chart.

The Change Colors button (Excel 2013)
allows a quick change of color theme for a chart.

The colour options you get depend on the overall color theme that's in operation; you set this from the Page Layout menu.

Set overall color themes in Excel using the
Page Layout > Colors button.

Chart colors can still be modified afterwards.

Top

### Overall setting of colours in Excel

You can set the general colour theme using the Page Layout > Colors button. This sets the default colors for charts as well as other Excel items (such as headings in Pivot Tables). Once you've set an overall theme and created a chart you can alter the chart colors using the Design > Change Colors button.

The Page Layout > Colors button changes the overall Excel color theme.

Once you have a general color set you can of course still tinker with the individual colors by formating chart elements directly. Don't just settle for the defaults; your graphs are important and it is worth spending a little bit of time to get the "best" look you can. If you use basic defaults your chart will look lacklustre and people will think that your work is similarly lacking. Your results graphs are the most important aspect of your work, make them count!

Select individual chart elements to set color explicitly.

A Pattern Fill can be especially helpful for monochrome charts.

Top

### Setting colour explicitly in Excel charts

Whatever color theme you have set or applied you can always alter the individual colors of the chart elements. Usually this means altering colors for the various data series but you can also set colors for axis lines and labels.

The most reliable method of selecting a chart element is to use the Chart Tools > Format > Format Selection menu item. However, you can also right-click or double-click on the chart directly.

Once you have selected a data series you'll most likely have opened the Format Data Series dialogue box. You can alter the Fill settings by using solid colors and choosing the color you want.

You can also choose a Pattern Fill. The pattern is especially helpful with monochrome charts, such as those intended for paper publication.

The Pattern Fill is especially helpful for monochome charts

You can set the general color for the pattern (the Foreground button), the background defaults to white but you can set it separately if you like.

Keep chart "effects" to a minimum. Aim to make the chart more readable, not overly fancy.

Top

There are plenty of options for patterns, look to keep things simple though.

Using a Pattern Fill is helpful in monochome charts

Generally you want to avoid crazy effects, your aim is to help make the chart readable, not to assault the senses.

Use colors() to see all the named colors in R

Most graphical commands use an explicit default palette of colors

Top

### Using color in R plots

R has over 650 named colors to choose from. You can see the colors using the colors() command.

`> colors()  [1] "white"                "aliceblue"            "antiquewhite"          [4] "antiquewhite1"        "antiquewhite2"        "antiquewhite3"         [7] "antiquewhite4"        "aquamarine"           "aquamarine1"          [10] "aquamarine2"          "aquamarine3"          "aquamarine4"          [13] "azure"                "azure1"               "azure2"               [16] "azure3"               "azure4"               "beige"                [19] "bisque"               "bisque1"              "bisque2"              [22] "bisque3"              "bisque4"              "black"                [25] "blanchedalmond"       "blue"                 "blue1"                [28] "blue2"                "blue3"                "blue4"`

The colors used for most graphical commands are taken from a default palette, which can be different for different commands. For example the barplot() command uses shades of gray whilst the pie() command uses a palette of pastel shades.

In most graphical commands you can set the colors for the plot using the col parameter. Colors can be named explicitly or given numbers, in which case the numbers are taken to be the colors of the existing color palette().

Specify names (in quotes) using the col parameter in graphical commands.

Top

### Specifying color

You can set the colors of an R plot in several ways:

• Give color names (in quotes) as in the colors() command.
• Give numbers – the numbers will refer to the colors in the currently set palette().
• Give the name of a built-in color palette (and the number of shades).

You can set the colors directly in a plotting command using the col parameter and a vector of names (in quotes), for example:

```> barplot(VADeaths, beside = TRUE,
col = c("aliceblue", "bisque", "coral", "seagreen", "tomato"))```

If you plan to use the colors more than once you might want to save your vector of colors as a named object.

`> mycol = c("aliceblue", "bisque", "coral", "seagreen", "tomato")> pie(VADeaths[1,], col = mycol)`

If you don't provide enough colors then they are recycled. If there are too many colors the un-required ones are ignored.

If you give the colors as numbers the plotting command will take the colors from their position in the vector of colors that are in the color palette(), not the position of the color from the colors() command.

You can also use one of the built-in palettes by specifying the palette name and the number of shades needed.

Use the palette() command to change default colors in R plotting comands.

The current palette() affects colors that are specified by number.

The number refers to the color position in the palette()

Use col = palette() to over-ride defaults in graphical plotting commands.

Reset the palette with palette("default")

Top

### Setting a color palette

The palette() command allows you to set a default color palette. The default palette() can be viewed using an empty command:

`> palette()[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" [8] "gray" `

Now, whenever a color is referred to by an integer value the color is taken as the position in the current palette(). Some plotting commands have their own default colors, which are used if you do not specify the colors explicitly.

• The barplot() command uses a palette() of gray.
• The pie() command uses a palette() of pastel shades.

You can set the palette() by giving a vector of colors:

`> palette(mycol)> palette()[1] "aliceblue" "bisque"    "coral"     "seagreen"  "tomato" `

To restore the default palette():

> palette("default")

To use the colors in your palette() you can either give the numbers of the position of the colors in the palette() or specify col = palette().

`> palette()[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" [8] "gray"   > mycol[1] "aliceblue" "bisque"    "coral"     "seagreen"  "tomato"   > palette(mycol)> pie(VADeaths[,1], col = palette())`

The pie() command uses a set range of colors unless told otherwise
Here a custom palette() has been used

Once you have set a palette() the colors remain "operational" until you change the palette(). Any colors referred to by number will refer to the current palette() but of course you can over-ride the palette() by specifying colors by name.

R contains several built-in palettes:

rainbow()
heat.colors()
terrain.colors()
topo.colors()
cm.colors()
gray.colors()

Give the number of colors required to produce a co-ordinated set of colors.

For rainbow() and gray.colors() you can specify starting and ending values.

Top

### Built-in color palettes

You can make your own color palette() by specifying color names explicitly. However, R has several built-in palettes that you can use to crete a series of "co-ordinated" colors.

R contains several built-in color palettes that you can use to make sets of co-ordinated color

There are six built-in palettes.

• rainbow()
• heat.colors()
• terrain.colors()
• topo.colors()
• cm.colors()
• gray.colors()

In general you specify how many colors you require in the palette and the command produces a series of colors graded across the range, however, with rainbow() and gray.colors() you can specify starting and ending points.

Use density and angle parameters.

Top

In some chart types it may be helpful to have shading lines, especially when using monochromatic color palettes. The barplot() and pie() commands allow you to specify shading lines from withing the command. There are two parameters:

• density – gives the density of the shading lines in lines per inch. If not specified or negative the color is solid, even if angle is given.
• angle – gives the angle to draw the shading lines measured anti-clockwise, 0 is horizontal and the default is 45 degrees.

Shading is not available in the boxplot() command. If is rather less useful there anyhow, as the bars are all labelled. It is possible to find a solution using the polygon() command but this is not for the faint hearted.

Use pattern fills for monocrhome.

Top

## Summary

Don't just rely on the default colors for your graphs and charts. If you are presenting a graphic that uses color then think carefully about how easy it will be for readers to differentiate the colors. Think about the colors themselves, perhaps a particular color theme would be especially suitable for your presentation.

You can use color to highlight a particularly important finding, making some data stand out from the rest. You can also make some data "disappear" into obscurity!

For monochrome charts try a wide range so that individual series are identifiable. This is where pattern fills are especially useful.

Don't forget that the purpose of your chart is to help a reader understand the data/results easily.

Top

Providing training for:

• Ecology
• Data analysis
• Statistics
• R The statistical programming language
• Data management
• Data mining