ggplot(mpg, aes(x = hwy, y = cty, color = drv)) +
geom_point() +
labs(color = "Drive train")
-
+
If a legend is drawn for multiple aesthetics, you’ll want to update
the title for all of them.
-# not this
+#|
+# not this
ggplot(mpg, aes(x = hwy, y = cty, color = drv, shape = drv)) +
geom_point() +
labs(color = "Drive train")
-
+
# but this
ggplot(mpg, aes(x = hwy, y = cty, color = drv, shape = drv)) +
geom_point() +
labs(color = "Drive train", shape = "Drive train")
-
+
@@ -181,10 +173,7 @@
How can I increase t
legend.position = "bottom",
legend.spacing.x = unit(1.0, "cm")
)
-
+
For vertical legends changing legend.spacing.y
changes
the space between the legend title and the keys, but not between the
keys, e.g. see the large space between the legend title and keys.
@@ -192,10 +181,7 @@ How can I increase t
ggplot(mpg, aes(x = hwy, y = cty, color = drv)) +
geom_point() +
theme(legend.spacing.y = unit(3.0, "cm"))
-
+
In order to change the space between the legend keys, you can first
make the key size bigger with legend.key.size
and then
remove the grey background color with legend.key
.
@@ -206,11 +192,7 @@ How can I increase t
legend.key.size = unit(1.5, "cm"),
legend.key = element_rect(color = NA, fill = NA)
)
-
+
Note that the legend title is no longer aligned with the keys with
this approach. You can also shift it over with the hjust
setting of legend.title
.
@@ -222,11 +204,7 @@ How can I increase t
legend.key = element_rect(color = NA, fill = NA),
legend.title = element_text(hjust = 0.5)
)
-
+
@@ -253,11 +231,7 @@
How can I change the key
"f" = "Front-wheel drive",
"r" = "Rear-wheel drive")
)
-
+
@@ -283,10 +257,7 @@
How can I change the font
legend.text = element_text(size = 14),
legend.title = element_text(size = 10)
)
-
+
For further customization of legend text, see the documentation for
element_text()
, e.g. you can change font colors or font
face as well.
@@ -297,11 +268,7 @@ How can I change the font
legend.text = element_text(size = 14, color = "red"),
legend.title = element_text(size = 10, face = "bold.italic")
)
-
+
@@ -325,9 +292,7 @@ How can I change the bac
ggplot(mpg, aes(x = hwy, y = cty)) +
geom_point() +
theme(panel.background = element_rect(fill = "lightblue", colour = "red", linewidth = 3))
-
+
If you want to change the colour of the plotting area but not the
panel where the panel, you can so the same thing with
plot.background
.
@@ -335,10 +300,7 @@ How can I change the bac
ggplot(mpg, aes(x = hwy, y = cty)) +
geom_point() +
theme(plot.background = element_rect(fill = "lightblue", colour = "red", linewidth = 3))
-
+
Note that ggplot2 has a variety of complete
themes that might already do what you’re hoping to accomplish. For
example, if you prefer a more minimal look to your plots, without the
@@ -347,19 +309,14 @@
+
And you can continue customization based on one of these themes.
-
+
You might also find the thematic
package useful for simplified theming of your plots.
@@ -389,9 +346,7 @@ How can
-
+
You can change the color of NA
with
scale_fill_discrete()
in this case, e.g. make it
purple.
@@ -399,9 +354,7 @@
+
You can also set the color to "transparent"
. In the plot
below this is shown with theme_minimal()
to demonstrate how
that looks on a plot with a transparent background. Note that while this
@@ -413,10 +366,7 @@
+
@@ -437,9 +387,7 @@ How can I change the
ggplot(mpg, aes(x = hwy, y = cty, color = class)) +
geom_point() +
theme_gray(base_size = 18)
-
+
If you would like all plots within a session/document to use a
particular base size, you can set it with set_theme()
. Run
the following at the beginning of your session or include on top of your
@@ -473,10 +421,7 @@
How can I
plot.title = element_text(size = 20),
plot.subtitle = element_text(size = 15)
)
-
+
For further customization of plot title and subtitle, see the
documentation for element_text()
, e.g. you can change font
colors or font face as well.
@@ -491,10 +436,7 @@ How can I
plot.title = element_text(size = 20, color = "red"),
plot.subtitle = element_text(size = 15, face = "bold.italic")
)
-
+
@@ -521,10 +463,7 @@
How can I change the font
axis.title.x = element_text(size = 20),
axis.title.y = element_text(size = 10)
)
-
+
For further customization of plot title and subtitle, see the
documentation for element_text()
, e.g. you can change font
colors or font face as well.
@@ -539,10 +478,7 @@ How can I change the font
axis.title.x = element_text(size = 20, color = "red"),
axis.title.y = element_text(size = 10, face = "bold.italic")
)
-
+
You can also change the size of the axis text (e.g. numbers at the
axis ticks) using axis.text
(or axis.text.x
and axis.text.y
if you want to set different sizes).
@@ -557,10 +493,7 @@ How can I change the font
axis.title = element_text(size = 16),
axis.text = element_text(size = 20, color = "blue")
)
-
+
-
+
You can set the size of the text with the following.
-
+
Or you can map it to the size
aes
thetic. In
the following size is determined by the x
value with
scale_size_identity()
.
@@ -613,8 +544,7 @@
+
If you want to use the same updated size for geom_text()
in a series of plots in a session/R Markdown document, you can set use
update_geom_defaults()
to update the default size, e.g. if
diff --git a/dev/articles/faq-faceting.html b/dev/articles/faq-faceting.html
index 211d1b915a..a2901289bd 100644
--- a/dev/articles/faq-faceting.html
+++ b/dev/articles/faq-faceting.html
@@ -134,9 +134,7 @@
What is the di
geom_histogram() +
facet_wrap(~ drv)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-
+
And facet_grid()
is commonly used to facet by a plot by
two categorical variables.
@@ -144,10 +142,7 @@ What is the di
geom_histogram() +
facet_grid(cyl ~ drv)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-
+
Notice that this results in some empty panes (e.g. 4-wheel drive and
5 cylinders) as there are no cars in the mpg
dataset that
fall into such categories.
@@ -159,9 +154,7 @@ What is the di
geom_histogram() +
facet_wrap(cyl ~ drv)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-
+
In facet_wrap()
you can control the number of rows
and/or columns of the resulting plot layout using the nrow
and ncol
arguments, respectively. In
@@ -178,16 +171,14 @@
What is the di
geom_histogram() +
facet_grid(cyl ~ .)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-
+
-
+
-
+
First, calculate these means and save them in a new data frame.
-
+
@@ -266,12 +252,7 @@ How can I set individua
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point() +
facet_grid(cyl ~ drv)
-
+
You can control this behaviour with the scales
argument
of faceting functions: varying scales across rows
("free_x"
), columns ("free_y"
), or both rows
@@ -280,13 +261,7 @@
How can I set individua
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point() +
facet_grid(cyl ~ drv, scales = "free")
-
+
If you also want to make sure that a particular value or range is
included in each of the facets, you can set this with
expand_limits()
, e.g. ensure that 10 is included in the
@@ -296,13 +271,7 @@
How can I set individua
geom_point() +
facet_grid(cyl ~ drv, scales = "free") +
expand_limits(x = 10, y = c(20, 25))
-
+
@@ -323,10 +292,7 @@
+
You can also remove the labels across rows only with
strip.x.text
or across columns only with
strip.y.text
.
@@ -335,10 +301,7 @@
+
-
+
You can control the maximum width of the facet label by setting the
width
in the label_wrap_gen()
function, which
is then passed to the labeller
argument of your faceting
@@ -379,11 +338,7 @@
+
@@ -429,9 +384,7 @@
How can I set different
facet_wrap(~ country, ncol = 1, scales = "free_y") +
scale_x_continuous(breaks = 2011:2020)
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
-
+
With the following you can customize the facet labels first with
as_labeller()
, turn off the default y-axis label, and then
place the facet labels where the y-axis label goes
@@ -452,10 +405,7 @@
How can I set different
#> returning -Inf
#> Warning in max(ids, na.rm = TRUE): no non-missing arguments to max;
#> returning -Inf
-
+
diff --git a/dev/articles/faq-reordering.html b/dev/articles/faq-reordering.html
index 54d91a432d..2dd9426d67 100644
--- a/dev/articles/faq-reordering.html
+++ b/dev/articles/faq-reordering.html
@@ -134,16 +134,12 @@ How can I reord
-
+
To do this, you can use forcats::fct_infreq()
.
-
+
If you’d like to plot the highest value first, you can also reverse
the order with forcats::fct_rev()
. You might also want to
simplify the axis label.
@@ -151,9 +147,7 @@ How can I reord
ggplot(mpg, aes(y = forcats::fct_rev(forcats::fct_infreq(class)))) +
geom_bar() +
labs(y = "class")
-
+
@@ -172,11 +166,7 @@
How can I reorder th
-
+
You can reverse the order clarity
levels are displayed
in the bars with forcats::fct_rev()
. This will also change
the order they’re presented in the legend so the two orders match.
@@ -184,11 +174,7 @@
How can I reorder th
ggplot(diamonds, aes(x = cut, fill = forcats::fct_rev(clarity))) +
geom_bar() +
labs(fill = "clarity")
-
+
@@ -212,9 +198,7 @@
+
Suppose you’d like the boxes to be ordered in ascending order of
their medians. You can do this in a data transformation step prior to
plotting (e.g. with dplyr::mutate()
) or you can do it
@@ -224,9 +208,7 @@
How can
ggplot(mpg, aes(x = forcats::fct_reorder(class, hwy, .fun = median), y = hwy)) +
geom_boxplot() +
labs(x = "class")
-
+
@@ -249,10 +231,7 @@
+
Suppose you’d like the panes to be in the order "r"
,
"f"
, "4"
. You can use
forcats::fct_relevel()
to reorder the levels of
@@ -263,10 +242,7 @@
+
@@ -299,10 +275,7 @@
+
Suppose you arranged your data in ascending order of the
x-coordinates and plotted again. Now the blue circle is over the yellow
triangle since 0.01 comes after 0 and similarly the red square is over
@@ -315,10 +288,7 @@
+
If you wanted to make sure that the observation identified with an
asterisk is always plotted on top, regardless of how the data are
arranged in the data frame, you can create an additional layer for that
@@ -329,10 +299,7 @@
+
ggplot(mapping = aes(x = x, y = y, fill = fill, shape = shape)) +
@@ -340,10 +307,7 @@
-
+
diff --git a/dev/articles/ggplot2-specs.html b/dev/articles/ggplot2-specs.html
index 0b1cd8f1c3..1bf17de9fc 100644
--- a/dev/articles/ggplot2-specs.html
+++ b/dev/articles/ggplot2-specs.html
@@ -173,9 +173,7 @@