theme_sg()
has arguments to control the legend position and appearance of grid lines, axis lines and axis ticks. More information on accepted values can be found in the help file.
To control the order of bars in a chart, wrap the variable you want to arrange with reorder()
and specify what variable you want to sort by. The following example sorts bars in ascending order of life expectancy. To sort in descending order, you would change this to reorder(country, desc(lifeExp))
.
#| fig.alt = "A bar chart using sgplot theme and dark blue colour with bars sorted in decreasing order by life expectancy." bar_data |> ggplot(aes(x = lifeExp, y = reorder(country, lifeExp))) + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg(axis = "y", grid = "x")
Examples in the following sections build on this chart.
Chart titles such as the main title, subtitle, caption, axis titles and legend titles, can be controlled using labs()
. A title can be removed using NULL
.
#| fig.alt = "A bar chart with title, subtitle and caption. Axis titles have been removed." last_plot() + labs( x = NULL, y = NULL, title = "Iceland has the highest life expectancy in Europe", subtitle = "Life expectancy in European countries, 2007", caption = "Source: Gapminder" )
By default, a bar chart will have a gap between the bottom of the bars and the axis. This can be removed as follows:
#| fig.alt = "A bar chart with no space between bottom of bars and x axis." last_plot() + scale_x_continuous(expand = c(0, 0))
The equivalent adjustment can be made for the y axis using scale_y_continuous
.
Axis limits, breaks and labels for continuous variables can be controlled using scale_x/y_continuous()
. For discrete variables, labels can be changes using scale_x/y_discrete()
or alternatively by recoding the variable in the data before creating a chart.
#| fig.alt = "A bar chart with fewer x axis breaks and edited labels." last_plot() + scale_x_continuous(expand = c(0, 0), limits = c(0, 85), breaks = seq(0, 80, 20), labels = c(seq(0, 70, 20), "80 years"))
Note that further calls to scale_x/y_continuous
will overwrite previous calls, hence why expand = c(0, 0)
has been included again in this example.
Formatting axis labels or legend labels is easily handled using the scales
package. The following example formats y axis labels as percentages, however scales
can also handle currency and thousands separators.
#| fig.alt = "A stacked bar chart with y axis labels formatted as percentages." stacked_bar_data |> ggplot(aes(x = continent, y = n_countries, fill = lifeExpGrouped)) + geom_bar(stat = "identity", position = "fill") + theme_sg(legend = "bottom") + scale_y_continuous(expand = c(0, 0), labels = scales::percent) + scale_fill_discrete_sg() + labs( x = NULL, y = NULL, fill = "Life Expectancy", title = "How life expectancy varies across continents", subtitle = "Percentage of countries by life expectancy band, 2007", caption = "Source: Gapminder" )
Axis lines and grid lines can sometimes appear 'cut off' if they are drawn at the limits of the chart range. You can see in the example in the previous section that the top grid line is slightly narrower than the adjacent tick mark on the y axis. This is because the y axis limit is 100%. As the grid line is centred at 100%, the top half of the line is 'cut off'. This can be corrected as follows:
#| fig.alt = "A stacked bar chart with top gridline the same width as adjoining tick mark and other grid lines." last_plot() + coord_cartesian(clip = "off")
To add a horizontal or vertical line across the whole plot, use geom_hline()
or geom_vline()
. This can be useful to highlight a threshold or average level.
#| fig.alt = "A line chart with dashed, orange horizontal line at age 75." gapminder |> filter(country == "United Kingdom") |> ggplot(aes(x = year, y = lifeExp)) + geom_line(linewidth = 1, colour = sg_colour_values[1]) + geom_hline(yintercept = 75, colour = sg_colour_values[2], linewidth = 1, linetype = "dashed") + annotate(geom = "text", x = 2007, y = 70, label = "Age 70") + theme_sg() + scale_y_continuous(limits = c(0, 82), breaks = seq(0, 80, 20), expand = c(0, 0)) + scale_x_continuous(breaks = seq(1952, 2007, 5)) + labs( x = "Year", y = NULL, title = "Living Longer", subtitle = "Life Expectancy in the United Kingdom 1952-2007", caption = "Source: Gapminder" )
If text is too long, it may be cut off or distort the dimensions of the chart.
#| fig.alt = "A bar chart with end of main title text not visible and long y axis title skewing the dimensions of the chart." plot <- ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg() + scale_y_continuous(expand = c(0, 0)) + labs( x = NULL, subtitle = "Life expectancy in European countries, 2007", caption = "Source: Gapminder" ) plot + labs( y = "Percentage of countries", title = paste("Iceland has the highest life expectancy in Europe", "followed closely by Switzerland") )
There are two suggested ways to solve this issue; Insert \n
within a string to force a line break; Use stringr::str_wrap()
to set a maximum character width of the string. See examples of both of these methods as follows:
#| fig.alt = "A bar chart with main title and y axis title text wrapped onto two lines." plot + labs( y = "Percentage\nof countries", title = stringr::str_wrap( paste("Iceland has the highest life expectancy in Europe", "followed closely by Switzerland"), width = 50 ) )
If you find you need to adjust theme elements for your chart, this can be done using theme()
. Note that this should be done after the call to theme_sg()
, otherwise theme_sg()
may overwrite the specifications you've made.
#| fig.alt = "A bar chart using sgplot theme and dark blue colour, with axis lines and ticks coloured black." ggplot(bar_data, aes(x = reorder(country, -lifeExp), y = lifeExp)) + geom_col(fill = sg_colour_values["dark-blue"]) + theme_sg(axis = "xy") + theme(axis.line = element_line(colour = "black"), axis.ticks = element_line(colour = "black")) + scale_y_continuous(expand = c(0, 0)) + labs( x = NULL, y = NULL, title = "Iceland has the highest life expectancy in Europe", subtitle = "Life expectancy in European countries, 2007", caption = "Source: Gapminder" )
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.