Skip to content

Commit 6e57050

Browse files
authored
Merge branch 'main' into label_dictionary
2 parents 2f8d981 + 2bd8cd5 commit 6e57050

17 files changed

+232
-210
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* New argument `labs(dictionary)` to label based on variable name rather than
44
based on aesthetic (@teunbrand, #5178)
5+
* The `summary()` method for ggplots is now more terse about facets
6+
(@teunbrand, #5989).
57
* `guide_bins()`, `guide_colourbar()` and `guide_coloursteps()` gain an `angle`
68
argument to overrule theme settings, similar to `guide_axis(angle)`
79
(@teunbrand, #4594).

R/geom-ribbon.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
9898
GeomRibbon <- ggproto("GeomRibbon", Geom,
9999
default_aes = aes(
100100
colour = NA,
101-
fill = from_theme(col_mix(ink, paper, 0.799)),
101+
fill = from_theme(col_mix(ink, paper, 0.2)),
102102
linewidth = from_theme(borderwidth),
103103
linetype = from_theme(bordertype),
104104
alpha = NA),

R/geom-text.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#'
3232
#' @section Alignment:
3333
#' You can modify text alignment with the `vjust` and `hjust`
34-
#' aesthetics. These can either be a number between 0 (right/bottom) and
35-
#' 1 (top/left) or a character (`"left"`, `"middle"`, `"right"`, `"bottom"`,
34+
#' aesthetics. These can either be a number between 0 (left/bottom) and
35+
#' 1 (right/top) or a character (`"left"`, `"middle"`, `"right"`, `"bottom"`,
3636
#' `"center"`, `"top"`). There are two special alignments: `"inward"` and
3737
#' `"outward"`. Inward always aligns text towards the center, and outward
3838
#' aligns it away from the center.

R/summary.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ summary.ggplot <- function(object, ...) {
2929
cat("scales: ", paste(object$scales$input(), collapse = ", "), "\n")
3030
}
3131

32-
cat("faceting: ")
33-
print(object$facet)
32+
vars <- object$facet$vars()
33+
vars <- if (length(vars) > 0) paste0("~", vars) else "<empty>"
34+
cat("faceting: ", paste0(vars, collapse = ", "), "\n")
3435

3536
if (length(object$layers) > 0)
3637
cat("-----------------------------------\n")

README.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pak::pak("tidyverse/ggplot2")
4747
It's hard to succinctly describe how ggplot2 works because it embodies a deep philosophy of visualisation. However, in most cases you start with `ggplot()`, supply a dataset and aesthetic mapping (with `aes()`). You then add on layers (like `geom_point()` or `geom_histogram()`), scales (like `scale_colour_brewer()`), faceting specifications (like `facet_wrap()`) and coordinate systems (like `coord_flip()`).
4848

4949
```{r example}
50-
#| fig.alt = "Scatterplot of engine displacement versus highway miles per
50+
#| fig.alt: "Scatterplot of engine displacement versus highway miles per
5151
#| gallon, for 234 cars coloured by 7 'types' of car. The displacement and miles
5252
#| per gallon are inversely correlated."
5353
library(ggplot2)

man/geom_text.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/utilities.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,19 @@
5454

5555
Can't recycle `..1` (size 4) to match `..2` (size 0).
5656

57+
# summary method gives a nice summary
58+
59+
Code
60+
summary(p)
61+
Output
62+
data: manufacturer, model, displ, year, cyl, trans, drv, cty, hwy, fl,
63+
class [234x11]
64+
mapping: x = ~displ, y = ~hwy, colour = ~drv
65+
scales: x, xmin, xmax, xend, xintercept, xmin_final, xmax_final, xlower, xmiddle, xupper, x0, colour
66+
faceting: ~year, ~cyl
67+
-----------------------------------
68+
geom_point: na.rm = FALSE
69+
stat_identity: na.rm = FALSE
70+
position_identity
71+
72+

tests/testthat/test-utilities.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,16 @@ test_that("expose/ignore_data() can round-trip a data.frame", {
196196
expect_equal(test, df[, c("a", "c", "b", "d")])
197197

198198
})
199+
200+
test_that("summary method gives a nice summary", {
201+
# This test isn't important enough to break anything on CRAN
202+
skip_on_cran()
203+
204+
p <- ggplot(mpg, aes(displ, hwy, colour = drv)) +
205+
geom_point() +
206+
scale_x_continuous() +
207+
scale_colour_brewer() +
208+
facet_grid(year ~ cyl)
209+
210+
expect_snapshot(summary(p))
211+
})

vignettes/articles/faq-annotation.Rmd

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You should use `annotate(geom = "text")` instead of `geom_text()` for annotation
3939
In the following visualisation we have annotated a histogram with a red line and red text to mark the mean. Note that both the line and the text appears pixellated/fuzzy.
4040

4141
```{r}
42-
#| fig.alt = "Histogram of highway miles per gallon for 234 cars. A red line is
42+
#| fig.alt: "Histogram of highway miles per gallon for 234 cars. A red line is
4343
#| placed at the position 23.44 and is adorned with the label 'mean 23.44'.
4444
#| Both the line and the text appear pixellated due to overplotting."
4545
mean_hwy <- round(mean(mpg$hwy), 2)
@@ -62,7 +62,7 @@ This is because `geom_text()` draws the geom once per each row of the data frame
6262

6363

6464
```{r}
65-
#| fig.alt = "Histogram of highway miles per gallon for 234 cars. A red line is
65+
#| fig.alt: "Histogram of highway miles per gallon for 234 cars. A red line is
6666
#| placed at the position 23.44 and is adorned with the label 'mean = 23.44'.
6767
#| Both the line and the text appear crisp."
6868
ggplot(mpg, aes(x = hwy)) +
@@ -91,7 +91,7 @@ Set `vjust = "inward"` and `hjust = "inward"` in `geom_text()`.
9191
Suppose you have the following data frame and visualization. The labels at the edges of the plot are cut off slightly.
9292

9393
```{r}
94-
#| fig.alt = "A plot showing the words 'two', 'three' and 'four' arranged
94+
#| fig.alt: "A plot showing the words 'two', 'three' and 'four' arranged
9595
#| diagonally. The 'two' and 'four' labels have been clipped to the panel's
9696
#| edge and are not displayed completely."
9797
df <- tibble::tribble(
@@ -108,7 +108,7 @@ ggplot(df, aes(x = x, y = y, label = name)) +
108108
You could manually extend axis limits to avoid this, but a more straightforward approach is to set `vjust = "inward"` and `hjust = "inward"` in `geom_text()`.
109109

110110
```{r}
111-
#| fig.alt = "A plot showing the words 'two', 'three' and 'four' arranged
111+
#| fig.alt: "A plot showing the words 'two', 'three' and 'four' arranged
112112
#| diagonally. The 'two' and 'four' labels are aligned to the top-right and
113113
#| bottom-left relative to their anchor points, and are displayed in their
114114
#| entirety."
@@ -129,7 +129,7 @@ Either calculate the counts ahead of time and place them on bars using `geom_tex
129129
Suppose you have the following bar plot and you want to add the number of cars that fall into each `drv` level on their respective bars.
130130

131131
```{r}
132-
#| fig.alt = "A bar chart showing the number of cars for each of three types
132+
#| fig.alt: "A bar chart showing the number of cars for each of three types
133133
#| of drive train."
134134
ggplot(mpg, aes(x = drv)) +
135135
geom_bar()
@@ -139,7 +139,7 @@ One option is to calculate the counts with `dplyr::count()` and then pass them t
139139
Note that we expanded the y axis limit to get the numbers to fit on the plot.
140140

141141
```{r}
142-
#| fig.alt = "A bar chart showing the number of cars for each of three types
142+
#| fig.alt: "A bar chart showing the number of cars for each of three types
143143
#| of drive train. The count values are displayed on top of the bars as text."
144144
mpg %>%
145145
dplyr::count(drv) %>%
@@ -152,7 +152,7 @@ mpg %>%
152152
Another option is to let `ggplot()` do the counting for you, and access these counts with `after_stat(count)` that is mapped to the labels to be placed on the plot with `stat_count()`.
153153

154154
```{r}
155-
#| fig.alt = "A bar chart showing the number of cars for each of three types
155+
#| fig.alt: "A bar chart showing the number of cars for each of three types
156156
#| of drive train. The count values are displayed on top of the bars as text."
157157
ggplot(mpg, aes(x = drv)) +
158158
geom_bar() +
@@ -173,7 +173,7 @@ First calculate the counts for each segment (e.g. with `dplyr::count()`) and the
173173
Suppose you have the following stacked bar plot.
174174

175175
```{r}
176-
#| fig.alt = "A stacked bar chart showing the number of cars for each of seven
176+
#| fig.alt: "A stacked bar chart showing the number of cars for each of seven
177177
#| types of cars. The fill colour of the bars indicate the type of drive
178178
#| train."
179179
ggplot(mpg, aes(x = class, fill = drv)) +
@@ -190,7 +190,7 @@ mpg %>%
190190
You can then pass this result directly to `ggplot()`, draw the segments with appropriate heights with `y = n` in the `aes`thetic mapping and `geom_col()` to draw the bars, and finally place the counts on the plot with `geom_text()`.
191191

192192
```{r}
193-
#| fig.alt = "A stacked bar chart showing the number of cars for each of seven
193+
#| fig.alt: "A stacked bar chart showing the number of cars for each of seven
194194
#| types of cars. The fill colour of the bars indicate the type of drive
195195
#| train. In the middle of each filled part, the count value is displayed as
196196
#| text."
@@ -214,7 +214,7 @@ Either calculate the proportions ahead of time and place them on bars using `geo
214214
Suppose you have the following bar plot but you want to display the proportion of cars that fall into each `drv` level, instead of the count.
215215

216216
```{r}
217-
#| fig.alt = "A bar chart showing the number of cars for each of three types
217+
#| fig.alt: "A bar chart showing the number of cars for each of three types
218218
#| of drive train."
219219
ggplot(mpg, aes(x = drv)) +
220220
geom_bar()
@@ -223,7 +223,7 @@ ggplot(mpg, aes(x = drv)) +
223223
One option is to calculate the proportions with `dplyr::count()` and then use `geom_col()` to draw the bars
224224

225225
```{r}
226-
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
226+
#| fig.alt: "A bar chart showing the proportion of cars for each of three types
227227
#| of drive train."
228228
mpg %>%
229229
dplyr::count(drv) %>%
@@ -236,7 +236,7 @@ Another option is to let `ggplot()` do the calculation of proportions for you, a
236236
Note that we also need to the `group = 1` mapping for this option.
237237

238238
```{r}
239-
#| fig.alt = "A bar chart showing the proportion of cars for each of three types
239+
#| fig.alt: "A bar chart showing the proportion of cars for each of three types
240240
#| of drive train."
241241
ggplot(mpg, aes(x = drv, y = ..prop.., group = 1)) +
242242
geom_bar()

0 commit comments

Comments
 (0)