3.6 Adjusting Bar Width and Spacing

3.6.1 Problem

You want to adjust the width of bars and the spacing between them.

3.6.2 Solution

To make the bars narrower or wider, set width in geom_col(). The default value is 0.9; larger values make the bars wider, and smaller values make the bars narrower (Figure 3.13).

For example, for standard-width bars:

library(gcookbook) # Load gcookbook for the pg_mean data set

ggplot(pg_mean, aes(x = group, y = weight)) +
  geom_col()

For narrower bars:

ggplot(pg_mean, aes(x = group, y = weight)) +
  geom_col(width = 0.5)

And for wider bars (these have the maximum width of 1):

ggplot(pg_mean, aes(x = group, y = weight)) +
  geom_col(width = 1)
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'group' with labels ctrl, trt1 and trt2.
#> It has y-axis 'weight' with labels 0, 2 and 4.
#> The chart is a bar chart with 3 vertical bars.
#> Bar 1 is centered horizontally at ctrl, and spans vertically from 0 to 5.03.
#> Bar 2 is centered horizontally at trt1, and spans vertically from 0 to 4.66.
#> Bar 3 is centered horizontally at trt2, and spans vertically from 0 to 5.53.
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'group' with labels ctrl, trt1 and trt2.
#> It has y-axis 'weight' with labels 0, 2 and 4.
#> The chart is a bar chart with 3 vertical bars.
#> Bar 1 is centered horizontally at ctrl, and spans vertically from 0 to 5.03.
#> Bar 2 is centered horizontally at trt1, and spans vertically from 0 to 4.66.
#> Bar 3 is centered horizontally at trt2, and spans vertically from 0 to 5.53.
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'group' with labels ctrl, trt1 and trt2.
#> It has y-axis 'weight' with labels 0, 2 and 4.
#> The chart is a bar chart with 3 vertical bars.
#> Bar 1 is centered horizontally at ctrl, and spans vertically from 0 to 5.03.
#> Bar 2 is centered horizontally at trt1, and spans vertically from 0 to 4.66.
#> Bar 3 is centered horizontally at trt2, and spans vertically from 0 to 5.53.
Different bar widthsDifferent bar widthsDifferent bar widths

Figure 3.13: Different bar widths

For grouped bars, the default is to have no space between bars within each group. To add space between bars within a group, make width smaller and set the value for position_dodge to be larger than width (Figure 3.14).

For a grouped bar graph with narrow bars:

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_col(width = 0.5, position = "dodge")

And with some space between the bars:

ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
  geom_col(width = 0.5, position = position_dodge(0.7))
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Date' with labels d16, d20 and d21.
#> It has y-axis 'Weight' with labels 0, 1, 2 and 3.
#> There is a legend indicating fill is used to show Cultivar, with 2 levels:
#> c39 shown as strong reddish orange fill and 
#> c52 shown as brilliant bluish green fill.
#> The chart is a bar chart with 6 vertical bars.
#> Bar 1 is centered horizontally at 0.88, and spans vertically from 0 to 3.18 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 2 is centered horizontally at 1.88, and spans vertically from 0 to 2.8 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 3 is centered horizontally at 2.88, and spans vertically from 0 to 2.74 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 4 is centered horizontally at 1.12, and spans vertically from 0 to 2.26 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 5 is centered horizontally at 2.12, and spans vertically from 0 to 3.11 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 6 is centered horizontally at 3.12, and spans vertically from 0 to 1.47 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Date' with labels d16, d20 and d21.
#> It has y-axis 'Weight' with labels 0, 1, 2 and 3.
#> There is a legend indicating fill is used to show Cultivar, with 2 levels:
#> c39 shown as strong reddish orange fill and 
#> c52 shown as brilliant bluish green fill.
#> The chart is a bar chart with 6 vertical bars.
#> Bar 1 is centered horizontally at 0.82, and spans vertically from 0 to 3.18 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 2 is centered horizontally at 1.82, and spans vertically from 0 to 2.8 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 3 is centered horizontally at 2.83, and spans vertically from 0 to 2.74 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 4 is centered horizontally at 1.18, and spans vertically from 0 to 2.26 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 5 is centered horizontally at 2.17, and spans vertically from 0 to 3.11 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 6 is centered horizontally at 3.17, and spans vertically from 0 to 1.47 with fill colour brilliant bluish green which maps to Cultivar = c52.
Bar graph with narrow grouped bars (left); With space between the bars (right)Bar graph with narrow grouped bars (left); With space between the bars (right)

Figure 3.14: Bar graph with narrow grouped bars (left); With space between the bars (right)

The first graph used position = "dodge", and the second graph used position = position_dodge(). This is because position = "dodge" is simply shorthand for position = position_dodge() with the default value of 0.9, but when we want to set a specific value, we need to use the more verbose form.

3.6.3 Discussion

The default width for bars is 0.9, and the default value used for position_dodge() is the same. To be more precise, the value of width in position_dodge() is NULL, which tells ggplot2 to use the same value as the width from geom_bar().

All of these will have the same result:

geom_bar(position = "dodge")
geom_bar(width = 0.9, position = position_dodge())
geom_bar(position = position_dodge(0.9))
geom_bar(width = 0.9, position = position_dodge(width=0.9))

The items on the x-axis have x values of 1, 2, 3, and so on, though you typically don’t refer to them by these numerical values. When you use geom_bar(width = 0.9), it makes each group take up a total width of 0.9 on the x-axis. When you use position_dodge(width = 0.9), it spaces the bars so that the middle of each bar is right where it would be if the bar width were 0.9 and the bars were touching. This is illustrated in Figure 3.15. The two graphs both have the same dodge width of 0.9, but while the top has a bar width of 0.9, the bottom has a bar width of 0.2. Despite the different bar widths, the middles of the bars stay aligned.

#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Date' with labels d16, d20 and d21.
#> It has y-axis 'Weight' with labels 0, 1, 2 and 3.
#> There is a legend indicating fill is used to show Cultivar, with 2 levels:
#> c39 shown as strong reddish orange fill and 
#> c52 shown as brilliant bluish green fill.
#> The chart is a bar chart with 6 vertical bars.
#> Bar 1 is centered horizontally at 0.78, and spans vertically from 0 to 3.18 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 2 is centered horizontally at 1.77, and spans vertically from 0 to 2.8 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 3 is centered horizontally at 2.78, and spans vertically from 0 to 2.74 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 4 is centered horizontally at 1.23, and spans vertically from 0 to 2.26 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 5 is centered horizontally at 2.22, and spans vertically from 0 to 3.11 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 6 is centered horizontally at 3.22, and spans vertically from 0 to 1.47 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Date' with labels d16, d20 and d21.
#> It has y-axis 'Weight' with labels 0, 1, 2 and 3.
#> There is a legend indicating fill is used to show Cultivar, with 2 levels:
#> c39 shown as strong reddish orange fill and 
#> c52 shown as brilliant bluish green fill.
#> The chart is a bar chart with 6 vertical bars.
#> Bar 1 is centered horizontally at 0.78, and spans vertically from 0 to 3.18 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 2 is centered horizontally at 1.77, and spans vertically from 0 to 2.8 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 3 is centered horizontally at 2.78, and spans vertically from 0 to 2.74 with fill colour strong reddish orange which maps to Cultivar = c39.
#> Bar 4 is centered horizontally at 1.23, and spans vertically from 0 to 2.26 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 5 is centered horizontally at 2.22, and spans vertically from 0 to 3.11 with fill colour brilliant bluish green which maps to Cultivar = c52.
#> Bar 6 is centered horizontally at 3.22, and spans vertically from 0 to 1.47 with fill colour brilliant bluish green which maps to Cultivar = c52.
Same dodge width of 0.9, but different bar widths of 0.9 (left) and 0.2 (right)Same dodge width of 0.9, but different bar widths of 0.9 (left) and 0.2 (right)

Figure 3.15: Same dodge width of 0.9, but different bar widths of 0.9 (left) and 0.2 (right)

If you make the entire graph wider or narrower, the bar dimensions will scale proportionally. To see how this works, you can just resize the window in which the graphs appear. For information about controlling this when writing to a file, see Chapter ??.