Skip to content

Commit 2f8fe07

Browse files
committed
add explicit description of the geom_line() vs geom_path() dynamic
see #29
1 parent 06f6a84 commit 2f8fe07

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

vignettes/features.Rmd

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Select multiple x variables to stack:
6363

6464
```{r}
6565
# all examples shown in this document work the same way for a horizontal
66-
# stack simplfy by switching out the x and y assignments
66+
# stack, simply switch out the x and y assignments
6767
mtcars |>
6868
ggstackplot(
6969
y = mpg, x = c(wt, qsec, drat)
@@ -421,7 +421,38 @@ mtcars |>
421421
geom_point(data = function(df) filter(df, .yvar == "drat")) +
422422
theme_stackplot()
423423
)
424-
424+
```
425+
426+
## Different plot elements
427+
428+
One can also change the geoms in the default theme. Here we use `geom_path()` instead of `geom_line()` in a horizontal stack. This is a very common use case because `geom_line()` connects the data points by increasing x-axis which is not always what we want (for example in oceanographic depth plots where we want to connect the data points by increasing y-axis value).
429+
430+
```{r}
431+
# horizontal stack with default (geom_line())
432+
mtcars |>
433+
ggstackplot(
434+
y = mpg, x = c(qsec, drat),
435+
color = c("#E41A1C", "#377EB8"),
436+
template =
437+
ggplot() +
438+
geom_point() +
439+
geom_line() + # default in template
440+
theme_stackplot()
441+
)
442+
# the following is the exact same data but using a
443+
# horizontal stack with "depth-profile" like geom_path()
444+
mtcars |>
445+
# arrange data by the y-axis
446+
arrange(mpg) |>
447+
ggstackplot(
448+
y = mpg, x = c(qsec, drat),
449+
color = c("#E41A1C", "#377EB8"),
450+
template =
451+
ggplot() +
452+
geom_point() +
453+
geom_path() + # plots data in order
454+
theme_stackplot()
455+
)
425456
```
426457

427458
## Additional plot elements
@@ -442,8 +473,6 @@ mtcars |>
442473
)
443474
```
444475

445-
446-
447476
## Axis modifications
448477

449478
Sometimes secondary axes will still be desired, especially if that axis is a transformation of an existing one. For example, here, we create a square root mpg axis that is plotted against the mpg axis. Again, all of this is defined in the `template` argument by adding a `scale_x_continuous` argument, just as you would in a normal ggplot.
@@ -464,7 +493,7 @@ mtcars |>
464493
name = "this is my mpg axis",
465494
# this can be the same with dup_axis() or as here have a transformed axis
466495
sec.axis = sec_axis(
467-
trans = sqrt,
496+
transform = sqrt,
468497
name = expression(sqrt(mpg)),
469498
breaks = scales::pretty_breaks(5)
470499
)
@@ -621,7 +650,7 @@ mtcars |>
621650
# Putting it all together
622651

623652
```{r, message = FALSE, fig.height=8}
624-
# example with economics data bundled with ggplot2
653+
# example from the README with economics data bundled with ggplot2
625654
ggplot2::economics |>
626655
ggstackplot(
627656
# define shared x axis
@@ -630,7 +659,8 @@ ggplot2::economics |>
630659
y = c(pce, pop, psavert, unemploy),
631660
# pick the RColorBrewer Dark2 palette (good color contrast)
632661
palette = "Dark2",
633-
# overlay the pce & pop plots (1), then make a full break (0) to the once again overlaye psavert & unemploy plots (1)
662+
# overlay the pce & pop plots (1), then make a full break (0) to the once
663+
# again overlaye psavert & unemploy plots (1)
634664
overlap = c(1, 0, 1),
635665
# switch axes so unemploy and psavert are on the side where they are
636666
# highest, respectively - not doing this here by changing the order of y
@@ -649,7 +679,7 @@ ggplot2::economics |>
649679
# add custom theme modifications, such as text size
650680
theme(text = element_text(size = 14)) +
651681
# make the shared axis a date axis
652-
scale_x_date() +
682+
scale_x_date("year") +
653683
# include y=0 for all plots to contextualize data better
654684
expand_limits(y = 0),
655685
# add plot specific elements
@@ -658,34 +688,32 @@ ggplot2::economics |>
658688
pce =
659689
# show pce in trillions of dollars
660690
scale_y_continuous(
661-
"personal consumption expenditures",
662-
labels = function(x) sprintf("$%.1f T", x/1000),
691+
"personal consumption expenditures",
663692
# always keep the secondary axis duplicated so ggstackplot can
664693
# manage axis placement for you
665-
sec.axis = dup_axis()
694+
sec.axis = dup_axis(),
695+
# labeling function for the dollar units
696+
labels = function(x) sprintf("$%.1f T", x/1000),
666697
),
667698
pop =
668699
# show population in millions
669700
scale_y_continuous(
670-
"population",
671-
labels = function(x) sprintf("%.0f M", x/1000),
672-
sec.axis = dup_axis()
701+
"population", sec.axis = dup_axis(),
702+
labels = function(x) sprintf("%.0f M", x/1000)
673703
),
674704
psavert =
675705
# savings is in %
676706
scale_y_continuous(
677-
"personal savings rate",
707+
"personal savings rate", sec.axis = dup_axis(),
678708
labels = function(x) paste0(x, "%"),
679-
sec.axis = dup_axis()
680709
) +
681710
# show data points in addition to line
682711
geom_point(),
683712
unemploy =
684713
# unemploy in millions
685714
scale_y_continuous(
686-
"unemployed persons",
687-
labels = function(x) sprintf("%.0f M", x/1000),
688-
sec.axis = dup_axis()
715+
"unemployed persons", sec.axis = dup_axis(),
716+
labels = function(x) sprintf("%.0f M", x/1000)
689717
) +
690718
# show data points in addition to line
691719
geom_point()

0 commit comments

Comments
 (0)