Skip to content

Commit 81311be

Browse files
Merge pull request #344 from UBC-DSCI/patch-viz-fig
moved legend to top and made it remain stacked vertically & fixed color palette
2 parents 5ec9630 + 6b40918 commit 81311be

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

viz.Rmd

Lines changed: 17 additions & 6 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,9 +777,11 @@ 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) +
773-
scale_y_log10(labels = comma)
783+
scale_y_log10(labels = comma) +
784+
scale_color_brewer(palette = "Set2")
774785
```
775786

776787
From the visualization in Figure \@ref(fig:scatter-color-by-category-palette),

0 commit comments

Comments
 (0)