Skip to content

Commit ebf88ca

Browse files
Alpha affects line geom_sf() (#3608)
Apply alpha to linestring geometries in geom_sf(). Fix #3589
1 parent 3870c12 commit ebf88ca

File tree

3 files changed

+56
-34
lines changed

3 files changed

+56
-34
lines changed

NEWS.md

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

9090
* Increase the default `nbin` of `guide_colourbar()` to place the ticks more precisely (#3508, @yutannihilation).
9191

92+
* `geom_sf()` now applies alpha to linestring geometries (#3589, @yutannihilation).
93+
9294
# ggplot2 3.2.1
9395

9496
This is a patch release fixing a few regressions introduced in 3.2.0 as well as

R/geom-sf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na.
167167
})
168168
alpha <- x$alpha %||% defaults$alpha[type_ind]
169169
col <- x$colour %||% defaults$colour[type_ind]
170-
col[is_point] <- alpha(col[is_point], alpha[is_point])
170+
col[is_point | is_line] <- alpha(col[is_point | is_line], alpha[is_point | is_line])
171171
fill <- x$fill %||% defaults$fill[type_ind]
172172
fill <- alpha(fill, alpha)
173173
size <- x$size %||% defaults$size[type_ind]

tests/testthat/test-geom-sf.R

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
context("geom-sf")
22

3+
test_that("geom_sf() removes rows containing missing aes", {
4+
skip_if_not_installed("sf")
5+
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
6+
7+
grob_xy_length <- function(x) {
8+
g <- layer_grob(x)[[1]]
9+
c(length(g$x), length(g$y))
10+
}
11+
12+
pts <- sf::st_sf(
13+
geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)),
14+
size = c(1, NA),
15+
shape = c("a", NA),
16+
colour = c("red", NA)
17+
)
18+
19+
p <- ggplot(pts) + geom_sf()
20+
expect_warning(
21+
expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)),
22+
"Removed 1 rows containing missing values"
23+
)
24+
expect_warning(
25+
expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)),
26+
"Removed 1 rows containing missing values"
27+
)
28+
# default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed
29+
expect_warning(
30+
expect_identical(grob_xy_length(p + aes(colour = colour) + scale_colour_identity()),
31+
c(1L, 1L)),
32+
"Removed 1 rows containing missing values"
33+
)
34+
})
35+
36+
test_that("geom_sf() handles alpha properly", {
37+
skip_if_not_installed("sf")
38+
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
39+
40+
sfc <- sf::st_sfc(
41+
sf::st_point(0:1),
42+
sf::st_linestring(rbind(0:1, 1:2)),
43+
sf::st_polygon(list(rbind(0:1, 1:2, 2:1, 0:1)))
44+
)
45+
red <- "#FF0000FF"
46+
p <- ggplot(sfc) + geom_sf(colour = red, fill = red, alpha = 0.5)
47+
g <- layer_grob(p)[[1]]
48+
49+
# alpha affects the colour of points and lines
50+
expect_equal(g[[1]]$gp$col, alpha(red, 0.5))
51+
expect_equal(g[[2]]$gp$col, alpha(red, 0.5))
52+
# alpha doesn't affect the colour of polygons, but the fill
53+
expect_equal(g[[3]]$gp$col, alpha(red, 1.0))
54+
expect_equal(g[[3]]$gp$fill, alpha(red, 0.5))
55+
})
356

457
# Visual tests ------------------------------------------------------------
558

@@ -64,36 +117,3 @@ test_that("geom_sf_text() and geom_sf_label() draws correctly", {
64117
ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
65118
)
66119
})
67-
68-
test_that("geom_sf() removes rows containing missing aes", {
69-
skip_if_not_installed("sf")
70-
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")
71-
72-
grob_xy_length <- function(x) {
73-
g <- layer_grob(x)[[1]]
74-
c(length(g$x), length(g$y))
75-
}
76-
77-
pts <- sf::st_sf(
78-
geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)),
79-
size = c(1, NA),
80-
shape = c("a", NA),
81-
colour = c("red", NA)
82-
)
83-
84-
p <- ggplot(pts) + geom_sf()
85-
expect_warning(
86-
expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)),
87-
"Removed 1 rows containing missing values"
88-
)
89-
expect_warning(
90-
expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)),
91-
"Removed 1 rows containing missing values"
92-
)
93-
# default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed
94-
expect_warning(
95-
expect_identical(grob_xy_length(p + aes(colour = colour) + scale_colour_identity()),
96-
c(1L, 1L)),
97-
"Removed 1 rows containing missing values"
98-
)
99-
})

0 commit comments

Comments
 (0)