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 8 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ up correct aspect ratio, and draws a graticule.
`panel_params` (#1311). These are parameters of the Coord that vary from
panel to panel.

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

## Minor bug fixes and improvements

### Facetting
Expand Down
56 changes: 54 additions & 2 deletions R/geom-point.r
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ geom_point <- function(mapping = NULL, data = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
dots <- list(...)
Copy link
Member

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.

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,
Expand All @@ -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
)
)
}
Expand Down Expand Up @@ -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 ",
Copy link
Member

Choose a reason for hiding this comment

The 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]))
}
6 changes: 6 additions & 0 deletions R/scale-manual.r
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The 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 translate_shape_string(). An easy way do that would be to combine pch_table with setNames(letters, letter)

orig_names <- names(values)
values <- translate_shape_string(values)
names(values) <- orig_names
}

manual_scale("shape", values, ...)
}

Expand Down