Skip to content

Alpha affects line geom_sf() #3608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@

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

* `geom_sf()` now applies alpha to linestring geometries (#3589, @yutannihilation).

# ggplot2 3.2.1

This is a patch release fixing a few regressions introduced in 3.2.0 as well as
Expand Down
2 changes: 1 addition & 1 deletion R/geom-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10, na.
})
alpha <- x$alpha %||% defaults$alpha[type_ind]
col <- x$colour %||% defaults$colour[type_ind]
col[is_point] <- alpha(col[is_point], alpha[is_point])
col[is_point | is_line] <- alpha(col[is_point | is_line], alpha[is_point | is_line])
fill <- x$fill %||% defaults$fill[type_ind]
fill <- alpha(fill, alpha)
size <- x$size %||% defaults$size[type_ind]
Expand Down
86 changes: 53 additions & 33 deletions tests/testthat/test-geom-sf.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
context("geom-sf")

test_that("geom_sf() removes rows containing missing aes", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")

grob_xy_length <- function(x) {
g <- layer_grob(x)[[1]]
c(length(g$x), length(g$y))
}

pts <- sf::st_sf(
geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)),
size = c(1, NA),
shape = c("a", NA),
colour = c("red", NA)
)

p <- ggplot(pts) + geom_sf()
expect_warning(
expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)),
"Removed 1 rows containing missing values"
)
expect_warning(
expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)),
"Removed 1 rows containing missing values"
)
# default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed
expect_warning(
expect_identical(grob_xy_length(p + aes(colour = colour) + scale_colour_identity()),
c(1L, 1L)),
"Removed 1 rows containing missing values"
)
})

test_that("geom_sf() handles alpha properly", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")

sfc <- sf::st_sfc(
sf::st_point(0:1),
sf::st_linestring(rbind(0:1, 1:2)),
sf::st_polygon(list(rbind(0:1, 1:2, 2:1, 0:1)))
)
red <- "#FF0000FF"
p <- ggplot(sfc) + geom_sf(colour = red, fill = red, alpha = 0.5)
g <- layer_grob(p)[[1]]

# alpha affects the colour of points and lines
expect_equal(g[[1]]$gp$col, alpha(red, 0.5))
expect_equal(g[[2]]$gp$col, alpha(red, 0.5))
# alpha doesn't affect the colour of polygons, but the fill
expect_equal(g[[3]]$gp$col, alpha(red, 1.0))
expect_equal(g[[3]]$gp$fill, alpha(red, 0.5))
})

# Visual tests ------------------------------------------------------------

Expand Down Expand Up @@ -64,36 +117,3 @@ test_that("geom_sf_text() and geom_sf_label() draws correctly", {
ggplot() + geom_sf_label(data = nc_3857, aes(label = NAME))
)
})

test_that("geom_sf() removes rows containing missing aes", {
skip_if_not_installed("sf")
if (packageVersion("sf") < "0.5.3") skip("Need sf 0.5.3")

grob_xy_length <- function(x) {
g <- layer_grob(x)[[1]]
c(length(g$x), length(g$y))
}

pts <- sf::st_sf(
geometry = sf::st_sfc(sf::st_point(0:1), sf::st_point(1:2)),
size = c(1, NA),
shape = c("a", NA),
colour = c("red", NA)
)

p <- ggplot(pts) + geom_sf()
expect_warning(
expect_identical(grob_xy_length(p + aes(size = size)), c(1L, 1L)),
"Removed 1 rows containing missing values"
)
expect_warning(
expect_identical(grob_xy_length(p + aes(shape = shape)), c(1L, 1L)),
"Removed 1 rows containing missing values"
)
# default colour scale maps a colour even to a NA, so identity scale is needed to see if NA is removed
expect_warning(
expect_identical(grob_xy_length(p + aes(colour = colour) + scale_colour_identity()),
c(1L, 1L)),
"Removed 1 rows containing missing values"
)
})