You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
)
425
456
```
426
457
427
458
## Additional plot elements
@@ -442,8 +473,6 @@ mtcars |>
442
473
)
443
474
```
444
475
445
-
446
-
447
476
## Axis modifications
448
477
449
478
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 |>
464
493
name = "this is my mpg axis",
465
494
# this can be the same with dup_axis() or as here have a transformed axis
466
495
sec.axis = sec_axis(
467
-
trans = sqrt,
496
+
transform = sqrt,
468
497
name = expression(sqrt(mpg)),
469
498
breaks = scales::pretty_breaks(5)
470
499
)
@@ -621,7 +650,7 @@ mtcars |>
621
650
# Putting it all together
622
651
623
652
```{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
625
654
ggplot2::economics |>
626
655
ggstackplot(
627
656
# define shared x axis
@@ -630,7 +659,8 @@ ggplot2::economics |>
630
659
y = c(pce, pop, psavert, unemploy),
631
660
# pick the RColorBrewer Dark2 palette (good color contrast)
632
661
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)
634
664
overlap = c(1, 0, 1),
635
665
# switch axes so unemploy and psavert are on the side where they are
636
666
# highest, respectively - not doing this here by changing the order of y
@@ -649,7 +679,7 @@ ggplot2::economics |>
649
679
# add custom theme modifications, such as text size
650
680
theme(text = element_text(size = 14)) +
651
681
# make the shared axis a date axis
652
-
scale_x_date() +
682
+
scale_x_date("year") +
653
683
# include y=0 for all plots to contextualize data better
654
684
expand_limits(y = 0),
655
685
# add plot specific elements
@@ -658,34 +688,32 @@ ggplot2::economics |>
658
688
pce =
659
689
# show pce in trillions of dollars
660
690
scale_y_continuous(
661
-
"personal consumption expenditures",
662
-
labels = function(x) sprintf("$%.1f T", x/1000),
691
+
"personal consumption expenditures",
663
692
# always keep the secondary axis duplicated so ggstackplot can
0 commit comments