Skip to content

return a tibble with ggplot_build() and layer_data() #3264

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -56,6 +56,8 @@ core developer team.

## Minor improvements and bug fixes

* `ggplot_build()` and `layer_data()` now return data as `tibble`s (@malcolmbarrett, #3264)

* `cut_width()` now accepts `...` to pass further arguments to `base::cut.default()`
like `cut_number()` and `cut_interval()` already did (@cderv, #3055)

Expand Down
3 changes: 3 additions & 0 deletions R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ ggplot_build.ggplot <- function(plot) {
# Let Layout modify data before rendering
data <- layout$finish_data(data)

# Convert to tibble
data <- lapply(data, tibble::as_tibble)

structure(
list(data = data, layout = layout, plot = plot),
class = "ggplot_built"
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-geom-smooth.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("geom_smooth")

test_that("data is ordered by x", {
df <- data_frame(x = c(1, 5, 2, 3, 4), y = 1:5)
df <- data_frame(x = c(1, 5, 2, 3, 4), y = as.numeric(1:5))

ps <- ggplot(df, aes(x, y))+
geom_smooth(stat = "identity", se = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-scale-manual.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dat <- data_frame(g = c("B","A","A"))
p <- ggplot(dat, aes(g, fill = g)) + geom_bar()
col <- c("A" = "red", "B" = "green", "C" = "blue")

cols <- function(x) ggplot_build(x)$data[[1]][, "fill"]
cols <- function(x) ggplot_build(x)$data[[1]][["fill"]]

test_that("named values work regardless of order", {
fill_scale <- function(order) scale_fill_manual(values = col[order],
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-stat-sf-coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ test_that("stat_sf_coordinates() retrieves coordinates from sf objects", {

# point
df_point <- sf::st_sf(geometry = sf::st_sfc(sf::st_point(c(0, 0))))
expect_identical(comp_sf_coord(df_point)[, c("x", "y")], data_frame(x = 0, y = 0))
expect_equal(comp_sf_coord(df_point)[, c("x", "y")], data_frame(x = 0, y = 0))

# line
c_line <- rbind(c(-1, -1), c(1, 1))
df_line <- sf::st_sf(geometry = sf::st_sfc(sf::st_linestring(c_line)))
expect_identical(
expect_equal(
# Note that st_point_on_surface() does not return the centroid for
# `df_line`, which may be a bit confusing. So, use st_centroid() here.
comp_sf_coord(df_line, fun.geometry = sf::st_centroid)[, c("x", "y")],
Expand All @@ -25,11 +25,11 @@ test_that("stat_sf_coordinates() retrieves coordinates from sf objects", {
# polygon
c_polygon <- list(rbind(c(-1, -1), c(-1, 1), c(1, 1), c(1, -1), c(-1, -1)))
df_polygon <- sf::st_sf(geometry = sf::st_sfc(sf::st_polygon(c_polygon)))
expect_identical(comp_sf_coord(df_point)[, c("x", "y")], data_frame(x = 0, y = 0))
expect_equal(comp_sf_coord(df_point)[, c("x", "y")], data_frame(x = 0, y = 0))

# computed variables (x and y)
df_point <- sf::st_sf(geometry = sf::st_sfc(sf::st_point(c(1, 2))))
expect_identical(
expect_equal(
comp_sf_coord(df_point, aes(x = stat(x) + 10, y = stat(y) * 10))[, c("x", "y")],
data_frame(x = 11, y = 20)
)
Expand All @@ -43,5 +43,5 @@ test_that("stat_sf_coordinates() ignores Z and M coordinates", {
df_xym <- sf::st_sf(geometry = sf::st_sfc(sf::st_polygon(c_polygon, dim = "XYM")))
# Note that st_centroid() and st_point_on_surface() cannot handle M dimension since
# GEOS does not support it. The default fun.geometry should drop M.
expect_identical(comp_sf_coord(df_xym)[, c("x", "y")], data_frame(x = 0, y = 0))
expect_equal(comp_sf_coord(df_xym)[, c("x", "y")], data_frame(x = 0, y = 0))
})