-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from 15 commits
a12f81a
46b3d79
92ad484
f8812b7
8f6bae3
d874a8c
69af1f6
02a19ec
6d7dab9
0ea30b3
f160450
a96f312
bea09d6
d2de3eb
f3948a8
ec64f9c
487b603
d8dadce
4e79af7
aee9b47
4cac31d
acf9f18
2916e40
8e9e09f
747cf63
044de3e
1aa5a04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -144,3 +148,61 @@ 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)) { | ||
bad_string <- unique(shape_string[invalid_strings]) | ||
collapsed_names <- paste0(bad_string, collapse = "', '") | ||
stop( | ||
"Invalid shape name: '", collapsed_names, "'", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind having a read of http://style.tidyverse.org/error-messages.html and seeing if you can rewrite these errors to more closely follow the guidelines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the ambiguous shape names, I've just put in the number of matches that the string would give; I'm not sure, but it might be nicer to give something like the following? 'tri' matches: triangle, triangle open, and 3 others There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's probably a bit too much effort for this case |
||
call. = FALSE) | ||
} | ||
|
||
if (any(nonunique_strings)) { | ||
bad_string <- unique(shape_string[nonunique_strings]) | ||
collapsed_names <- paste0(bad_string, collapse = "', '") | ||
stop( | ||
"Non-unique shape name: '", collapsed_names, "'", | ||
call. = FALSE) | ||
} | ||
|
||
unname(pch_table[shape_match]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
context("translate_shape_string") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests should go in |
||
|
||
test_that("strings translate to their corresponding integers", { | ||
shape_strings <- c( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need to systematically test ever possible value - the main thing you want to achieve in this test is to make sure everything is plumbed together correctly. |
||
"square open", | ||
"circle open", | ||
"triangle open", | ||
"plus", | ||
"cross", | ||
"diamond open", | ||
"triangle down open", | ||
"square cross", | ||
"asterisk", | ||
"diamond plus", | ||
"circle plus", | ||
"star", | ||
"square plus", | ||
"circle cross", | ||
"square triangle", | ||
"square", | ||
"circle small", | ||
"triangle", | ||
"diamond", | ||
"circle", | ||
"bullet", | ||
"circle filled", | ||
"square filled", | ||
"diamond filled", | ||
"triangle filled", | ||
"triangle down filled" | ||
) | ||
|
||
expect_equal(translate_shape_string(shape_strings[1]), 0) | ||
expect_equal(translate_shape_string(shape_strings), 0:25) | ||
|
||
expect_equal(translate_shape_string(rep.int(shape_strings[1], 10)), | ||
rep.int(0, 10)) | ||
expect_equal(translate_shape_string(rep(shape_strings[1:3], 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("non-unique substrings of shape names raise an error", { | ||
shape_strings <- c( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly you only need to test a single string here, and in the next error (and you could probably combine the two tests) |
||
"triangle", | ||
"circle", | ||
"tri", | ||
"star", | ||
"asterisk", | ||
"cir" | ||
) | ||
|
||
nonunique_error <- "Non-unique shape name" | ||
|
||
expect_error(translate_shape_string("tri"), nonunique_error) | ||
expect_error(translate_shape_string(shape_strings[1:3]), nonunique_error) | ||
expect_error(translate_shape_string(shape_strings), nonunique_error) | ||
expect_error(translate_shape_string(sample(shape_strings)), nonunique_error) | ||
}) | ||
|
||
test_that("invalid shape names raise an error", { | ||
shape_strings <- c( | ||
"triangle", | ||
"circle", | ||
"void", | ||
"star", | ||
"another void", | ||
"asterisk" | ||
) | ||
|
||
invalid_error <- "Invalid shape name" | ||
|
||
expect_error(translate_shape_string("void"), invalid_error) | ||
expect_error(translate_shape_string(shape_strings[1:3]), invalid_error) | ||
expect_error(translate_shape_string(shape_strings), invalid_error) | ||
expect_error(translate_shape_string(sample(shape_strings)), invalid_error) | ||
}) |
There was a problem hiding this comment.
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)