Skip to content

Commit e4c160f

Browse files
committed
moved legend to top and made it remain stacked vertically
closes #312
1 parent b9e8a1e commit e4c160f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

viz.Rmd

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,17 @@ ggplot(can_lang, aes(x = most_at_home_percent,
710710

711711
The legend in Figure \@ref(fig:03-scatter-color-by-category)
712712
takes up valuable plot area.
713-
We can improve this by moving the legend title using the `legend.position`
714-
argument of the `theme` function.
715-
Here we set it to `"bottom"` to put the legend beneath the plot.
713+
We can improve this by moving the legend title using the `legend.position`
714+
and `legend.direction`
715+
arguments of the `theme` function.
716+
Here we set `legend.position` to `"top"` to put the legend above the plot
717+
and `legend.direction` to `"vertical"` so that the legend items remain
718+
vertically stacked on top of each other.
719+
When the `legend.position` is set to either `"top"` or `"bottom"`
720+
the default direction is to stack the legend items horizontally.
721+
However, that will not work well for this particular visualization
722+
because the legend labels are quite long
723+
and would run off the page if displayed this way.
716724

717725
```{r 03-scatter-color-by-category-legend-edit, warning=FALSE, fig.height=5.5, fig.cap = "Scatter plot of percentage of Canadians reporting a language as their mother tongue vs the primary language at home colored by language category with the legend edited."}
718726
ggplot(can_lang, aes(x = most_at_home_percent,
@@ -722,7 +730,8 @@ ggplot(can_lang, aes(x = most_at_home_percent,
722730
labs(x = "Language spoken most at home \n (percentage of Canadian residents)",
723731
y = "Mother tongue \n (percentage of Canadian residents)") +
724732
theme(text = element_text(size = 14),
725-
legend.position = "bottom") +
733+
legend.position = "top",
734+
legend.direction = "vertical") +
726735
scale_x_log10(labels = comma) +
727736
scale_y_log10(labels = comma)
728737
```
@@ -768,7 +777,8 @@ ggplot(can_lang, aes(x = most_at_home_percent,
768777
labs(x = "Language spoken most at home \n (percentage of Canadian residents)",
769778
y = "Mother tongue \n (percentage of Canadian residents)") +
770779
theme(text = element_text(size = 14),
771-
legend.position = "bottom") +
780+
legend.position = "top",
781+
legend.direction = "vertical") +
772782
scale_x_log10(labels = comma) +
773783
scale_y_log10(labels = comma)
774784
```

0 commit comments

Comments
 (0)