ggplot2
: Fine TuningClick on the three horizontally stacked lines at the bottom left corner of the slide, then you will see table of contents, and you can jump to the section you want
Hit letter “o” on your keyboard and you will have a panel view of all the slides
Figures we have created so far cannot be used for formal presentations or publications. They are simply too crude.
We need fine-tune raw figures before they are publishable.
You can control virtually every element of a figure under the ggplot2
framework.
Take a look at here for the complete list of options you can use to modify the theme of figures
Key
The most important thing is actually to know which part of a figure a theme option refers to (e.g., axis.text
)
Two types of operations
Operations to make your figures presentable can be categorized into two types:
Examples
For the y-axis title,
The axis title text itself (say “Corn Yield (bu/acre)”) falls under the content category.
The position of or the font size of the axis-title fall under the theme category
The content itself does not change when theme is altered.
Original
Altered
Original
Altered
Distinctions between the two types of actions are not always clear
But, typically, you use
scale_*()
function series to alter contentstheme()
function to alter the themeNote that there are shorthand convenience functions to alter figure contents for commonly altered parts of figures
We are going to build on this figure in this section:
We can use
scale_x_discrete()
/scale_x_continuous()
for x-axisscale_y_discrete()
/scale_y_continuous()
for y-axisto control the following elements of axes:
name
: an axis titlelimit
: the range of an axisbreaks
: axis ticks positionslabel
: axis texts at ticksNote
scale_x_discrete()
if x
is a discrete variable (not numeric) and scale_x_continuous()
if x
is a continuous variable (numeric).y
.Or just this,
Or just,
Or,
You can filter the data first and then use the filtered data.
breaks
: determines where the ticks are locatedlabels
: defines the texts at the ticksRun the following code to create gg_delay
, which you will build on.
Change the axes content to create the figure on the right using scale_x_continuous()
and scale_y_continuous()
.
Here are the list of changes you need to make:
Change the axes content to create the figure on the right. But, use scale_x_continuous()
only for changing the x-axis breaks.
Here are the list of changes you need to make:
We are going to build on this figure in this section:
Run the following code to create gg_delay
, which you will build on.
Change the legend contents to create the figure on the right. Using scale_*_brewer()
. You need to identify what goes into *
in scale_*_brewer()
.
Here are the list of changes you need to make:
Set2
When specifying the theme of figure elements, it is good to know the naming convention of figure elements:
For example:
axis.title
This refers to the title of both x- and y-axis. Any aesthetic theme you apply to this element will be reflected on the title of both x- and y-axis.
axis.title.x
This refers to the title of only x-axis. Any aesthetic theme you apply to this element will be reflected on the title of only x-axis.
So, basically appending .name
narrows down the scope of the figure elements the element name refers to.
There are common functions we use to specify the aesthetic nature of figure elements based on the type of the elements:
element_text()
: for text elements like axis.text
, axis.title
, legend.text
Inside the function, you specify things like font size, font family, angle, etc.
element_rect()
: for box-like elements like legend.background
, plot.background
, strip.background
Inside the function, you specify things like font background color, border line color, etc.
element_line()
: for line elements like panel.grid.major
, axis.line.x
Inside the function, you specify things like line thickness, line color, etc.
element_blank()
: any componentsIt makes the specified component disappear.
unit()
: for attributes of figure elements like legend.key.width
, legend.box.spacing
We are going to build on this figure in this section:
We can use them()
to change the aesthetics of legends. Some of the elements include
See here for the full list of options related to legends.
We will discuss how to change the color scheme of legends later in much detail.
This is what we will build on:
There are a bunch of pre-made themes from the ggplot2
and ggthemes
packages that can quickly change how figures look.
Install and library ggthemes
package first:
See the full list of pre-made themes here.
You can simply override parts of the pre-made theme by adding theme options like this (see more on this here):
So, you can pick the pre-made theme that looks the closest to what you would like, and then add on theme elements to the part you do not like.
We will build from this figure:
See here for the line types available.
You can create your own theme, save it, and then use it later.
Here, I am creating my own theme off of theme_economist()
, where axis titles and major panel grids are absent.
You can add my_theme
like below just like a regular pre-made theme:
Faceted figures have strip
elements that do no exist for non-faceted figures like
strip.background
strip.placement
strip.text
panel.spacing
We learn how to modify these elements.
Create a dataset for this section:
Create a faceted figure we will build on:
Instead of naming the color you want to use, you can use HEX color codes instead.
Direction
You could alternatively use the RGB codes, but I do not see any reasons to do so because the use of HEX codes is sufficient.
You can use HEX color codes for any color-related elements in a figure.
Try
Pick a Hex color and try it yourself.
The choice of color schemes for your figures are very important (not so much for academic journals …)
We use scale_A_B()
functions to for color specification:
color
or fill
)For example, consider the following code:
Since it is the color
aesthetic that we want to work on, A = color
.
There are many options for B. Indeed, there are so many that, it gets confusing!
scale_color_brewer()
(discrete)scale_color_distiller()
(continuous)scale_color_viridis_d()
(discrete)scale_color_viridis_c()
(continuous)scale_color_continuous()
(continuous)scale_color_discrete()
(discrete)scale_color_hue()
(discrete)One thing to remember is that you need to be aware of whether the aesthetic variable (here, corn_yield
) is numeric or not as that determines acceptable type of B.
We have four scale
functions for Viridis color map:
scale_color_viridis_c()
: for color
aesthetic with a continuous variablescale_color_viridis_d()
: for color
aesthetic with a discrete variablescale_fill_viridis_c()
: for fill
aesthetic with a continuous variablescale_fill_viridis_d()
: for fill
aesthetic with a discrete variableThere are five color scheme types under the Viridis color map:
magma
inferno
plasma
viridis
civiris
You can use option
to specify which one of them you want to use inside the scale
functions.
These color schemes are color-blind sage.
RColorBrewer
package provides a number of color palettes of three types:
We use two types of scale functions for the palettes:
scale_A_brewer()
: for discrete aesthetic variablescale_A_distiller()
: for continuous aesthetic variableGenerate a dataset for visualization:
Create a figure:
Sometimes, you just want to pick colors yourself. In that case, you can use
scale_color_manual()
scale_fill_manual()
Inside the scale_*_manual()
function, you provide a named vector where a sequence of group names and their corresponding colors are specified to the scale
function via the values
option.
For example, consider the box plot of corn yield for four states: Colorado, Kansas, Nebraska, and South Dakota. Then, a sample named vector looks like this:
Now that a named vector is created, you can do the following to impose the color scheme you just defined.
Define a named color vector:
Create a figure:
How
You can use scale_*_gradientn()
to create your own continuous color scale.
Syntax
colors
: a vector of colorsvalues
: a vector of numeric numbers ranging from 0 to 1limits
: define the lower and upper bounds of the scale barn
th value of colors
is used for the interval defined by n
th and n+1
th values in values
.
Create a figure:
In this example, green is dominant in the color bar because the interval [0.2, 0.9] is for "green"
in colors
, where the interval represents [130, 235] ([100 + (250-100), 100 + (250-100)]).