-
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 8 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 |
---|---|---|
|
@@ -98,6 +98,14 @@ geom_point <- function(mapping = NULL, data = NULL, | |
na.rm = FALSE, | ||
show.legend = NA, | ||
inherit.aes = TRUE) { | ||
dots <- list(...) | ||
if (hasArg("shape")) { | ||
# Numerics/Strings of length 1 should not be translated | ||
if (is.character(dots$shape) && nchar(dots$shape[1]) > 1) { | ||
dots$shape <- translate_shape_string(dots$shape) | ||
} | ||
} | ||
|
||
layer( | ||
data = data, | ||
mapping = mapping, | ||
|
@@ -106,9 +114,9 @@ geom_point <- function(mapping = NULL, data = NULL, | |
position = position, | ||
show.legend = show.legend, | ||
inherit.aes = inherit.aes, | ||
params = list( | ||
params = c( | ||
na.rm = na.rm, | ||
... | ||
dots | ||
) | ||
) | ||
} | ||
|
@@ -144,3 +152,47 @@ GeomPoint <- ggproto("GeomPoint", Geom, | |
|
||
draw_key = draw_key_point | ||
) | ||
|
||
translate_shape_string <- function(shape_string) { | ||
pch_table <- c( | ||
"0" = "square open", | ||
"1" = "circle open", | ||
"2" = "triangle open", | ||
"3" = "plus", | ||
"4" = "cross", | ||
"5" = "diamond open", | ||
"6" = "triangle down open", | ||
"7" = "square cross", | ||
"8" = "asterisk", | ||
"9" = "diamond plus", | ||
"10" = "circle plus", | ||
"11" = "star", | ||
"12" = "square plus", | ||
"13" = "circle cross", | ||
"14" = "square triangle", | ||
"15" = "square", | ||
"16" = "circle small", | ||
"17" = "triangle", | ||
"18" = "diamond", | ||
"19" = "circle", | ||
"20" = "bullet", | ||
"21" = "circle filled ", | ||
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. Trailing space |
||
"22" = "square filled", | ||
"23" = "diamond filled", | ||
"24" = "triangle filled", | ||
"25" = "triangle down filled" | ||
) | ||
|
||
shape_match <- pmatch(shape_string, pch_table, duplicates.ok = TRUE) | ||
|
||
if (any(is.na(shape_match))) { | ||
bad_string <- shape_string[is.na(shape_match)] | ||
collapsed_names <- paste0(bad_string, collapse = "', '") | ||
stop( | ||
"Invalid shape name: '", collapsed_names, "'", | ||
call. = FALSE | ||
) | ||
} | ||
|
||
as.integer(names(pch_table[shape_match])) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,12 @@ scale_size_manual <- function(..., values) { | |
#' @rdname scale_manual | ||
#' @export | ||
scale_shape_manual <- function(..., values) { | ||
if (is.character(values) && nchar(values[1]) > 1) { | ||
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. I think it would be better to incorporate the existing behaviour for single letters in to |
||
orig_names <- names(values) | ||
values <- translate_shape_string(values) | ||
names(values) <- orig_names | ||
} | ||
|
||
manual_scale("shape", values, ...) | ||
} | ||
|
||
|
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.
I don't think this is the right place to do translation - it would be better to do so during drawing.