Skip to content

Shapes provided via strings instead of integers #2338

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
merged 27 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a12f81a
Add shape string translate function
daniel-barnett Nov 9, 2017
46b3d79
Call translate_shape_string from geom_point if shape is a character
daniel-barnett Nov 9, 2017
92ad484
Call translate_shape_string from scale_shape_manual if character
daniel-barnett Nov 9, 2017
f8812b7
Tidy-up/changing pch strings
daniel-barnett Nov 15, 2017
8f6bae3
Updated NEWS.md
daniel-barnett Nov 15, 2017
d874a8c
Sryle fixes
daniel-barnett Nov 15, 2017
69af1f6
Style fixes
daniel-barnett Nov 15, 2017
02a19ec
Remove paste() from stop()
daniel-barnett Nov 15, 2017
6d7dab9
Update translate_shape_string() function
daniel-barnett Dec 8, 2017
0ea30b3
Remove shape string translation from manual scales (is superfluous)
daniel-barnett Dec 8, 2017
f160450
Add shape string translation to draw_key_point()
daniel-barnett Dec 8, 2017
a96f312
Strip names from translated shape integer vector
daniel-barnett Dec 9, 2017
bea09d6
translate_shape_string() tests
daniel-barnett Dec 9, 2017
d2de3eb
Merge branch 'master' of https://github.com/daniel-barnett/ggplot2
daniel-barnett Dec 9, 2017
f3948a8
Merge remote-tracking branch 'upstream/master'
daniel-barnett Dec 9, 2017
ec64f9c
Rename shape translate test file
daniel-barnett May 2, 2018
487b603
Fix style
daniel-barnett May 2, 2018
d8dadce
Adhere to error message guidelines
daniel-barnett May 2, 2018
4e79af7
Remove superfluous tests and merge two tests for invalidity
daniel-barnett May 2, 2018
aee9b47
Merge remote-tracking branch 'upstream/master'
daniel-barnett May 2, 2018
4cac31d
Adjust geom-point tests
daniel-barnett May 2, 2018
acf9f18
Remove merge remnants
daniel-barnett May 2, 2018
2916e40
Add "triangle square" as an alias for "square triangle" shape
daniel-barnett May 3, 2018
8e9e09f
Add shape name example in aesthetic vignette
daniel-barnett May 3, 2018
747cf63
Quick formatting adjustments
daniel-barnett May 3, 2018
044de3e
Merge remote-tracking branch 'upstream/master'
daniel-barnett May 10, 2018
1aa5a04
Replace startsWith for <= 3.2 compatibility
daniel-barnett May 10, 2018
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ up correct aspect ratio, and draws a graticule.
* `guide_train()`, `guide_merge()`, `guide_geom()`, and `guide_gengrob()`
are now exported as they are needed if you want to design your own guide.
They are not currently documented; use at your own risk (#2528).

* Shapes can now be provided using strings instead of integers (i.e.
`geom_point(shape = "diamond")`) (@daniel-barnett, #2075).

## Breaking changes

Expand All @@ -177,6 +180,7 @@ up correct aspect ratio, and draws a graticule.
use matrix-columns. These are rarely used but are produced by `scale()`;
to continue use `scale()` you'll need to wrap it with `as.numeric()`,
e.g. `as.numeric(scale(x))`.
>>>>>>> upstream/master
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed this


## Minor bug fixes and improvements

Expand Down
91 changes: 91 additions & 0 deletions R/geom-point.r
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ GeomPoint <- ggproto("GeomPoint", Geom,
),

draw_panel = function(data, panel_params, coord, na.rm = FALSE) {
if (is.character(data$shape)) {
data$shape <- translate_shape_string(data$shape)
}

coords <- coord$transform(data, panel_params)
ggname("geom_point",
pointsGrob(
Expand All @@ -144,3 +148,90 @@ GeomPoint <- ggproto("GeomPoint", Geom,

draw_key = draw_key_point
)

translate_shape_string <- function(shape_string) {
if (nchar(shape_string[1]) == 1) {
return(shape_string)
}

pch_table <- c(
"square open" = 0,
"circle open" = 1,
"triangle open" = 2,
"plus" = 3,
"cross" = 4,
"diamond open" = 5,
"triangle down open" = 6,
"square cross" = 7,
"asterisk" = 8,
"diamond plus" = 9,
"circle plus" = 10,
"star" = 11,
"square plus" = 12,
"circle cross" = 13,
"square triangle" = 14,
"square" = 15,
"circle small" = 16,
"triangle" = 17,
"diamond" = 18,
"circle" = 19,
"bullet" = 20,
"circle filled" = 21,
"square filled" = 22,
"diamond filled" = 23,
"triangle filled" = 24,
"triangle down filled" = 25
)

shape_match <- charmatch(shape_string, names(pch_table))

invalid_strings <- is.na(shape_match)
nonunique_strings <- shape_match == 0

if (any(invalid_strings)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please follow the standard tidyverse style here? (Main problem is that ) should go on own line; you can use styler package to do automatically)

bad_string <- unique(shape_string[invalid_strings])
n_bad <- length(bad_string)

collapsed_names <- sprintf("\n* '%s'", bad_string[1:min(5, n_bad)])

more_problems <- if (n_bad > 5) {
sprintf("\n* ... and %d more problem%s", n_bad - 5, ifelse(n_bad > 6, "s", ""))
}

stop(
"Can't find shape name:",
collapsed_names,
more_problems,
call. = FALSE
)
}

if (any(nonunique_strings)) {
bad_string <- unique(shape_string[nonunique_strings])
n_bad <- length(bad_string)

n_matches <- vapply(
bad_string[1:min(5, n_bad)],
function(shape_string) sum(startsWith(names(pch_table), shape_string)),
integer(1)
)

collapsed_names <- sprintf(
"\n* '%s' partially matches %d shape names",
bad_string[1:min(5, n_bad)], n_matches
)

more_problems <- if (n_bad > 5) {
sprintf("\n* ... and %d more problem%s", n_bad - 5, ifelse(n_bad > 6, "s", ""))
}

stop(
"Shape names must be unambiguous:",
collapsed_names,
more_problems,
call. = FALSE
)
}

unname(pch_table[shape_match])
}
4 changes: 4 additions & 0 deletions R/legend-draw.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ NULL
#' @export
#' @rdname draw_key
draw_key_point <- function(data, params, size) {
if (is.character(data$shape)) {
data$shape <- translate_shape_string(data$shape)
}

pointsGrob(0.5, 0.5,
pch = data$shape,
gp = gpar(
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-geom-point.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
context("translate_shape_string")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this should now become geom-point


test_that("strings translate to their corresponding integers", {
shape_strings <- c(
"square open",
"circle open",
"triangle open"
)

expect_equal(translate_shape_string(shape_strings[1]), 0)
expect_equal(translate_shape_string(shape_strings), 0:2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not immediately clear to me what the intent of these tests are — I think it's to check that translate_shape_string() is vectorised? If so, I think you should create a new test, and I think you should only need one expectation.


expect_equal(
translate_shape_string(rep.int(shape_strings[1], 10)),
rep.int(0, 10)
)

expect_equal(
translate_shape_string(rep(shape_strings, each = 4)),
rep(0:2, each = 4)
)
})

test_that("single characters are not translated to integers", {
expect_equal(translate_shape_string(letters), letters)
expect_equal(translate_shape_string(as.character(0:9)), as.character(0:9))
})

test_that("invalid shape names raise an error", {
expect_error(translate_shape_string("void"), "Can't find shape name")
expect_error(translate_shape_string("tri"), "Shape names must be unambiguous")
})