Skip to content

sketch some ideas for arrow() #936

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 1 commit 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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ S3method(to_basic,GeomRect)
S3method(to_basic,GeomRibbon)
S3method(to_basic,GeomSegment)
S3method(to_basic,GeomSmooth)
S3method(to_basic,GeomSpoke)
S3method(to_basic,GeomStep)
S3method(to_basic,GeomTile)
S3method(to_basic,GeomViolin)
Expand Down
6 changes: 3 additions & 3 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A
if ("datetime" %in% scaleName) forMat <- function(x) as.POSIXct(x / 1000, origin = "1970-01-01")
# convert "days from the UNIX epoch" to a date/datetime
if ("date" %in% scaleName) forMat <- function(x) as.Date(as.POSIXct(x * 86400, origin = "1970-01-01"))
} else {
if (aesName != "text") aesName <- paste0(aesName, "_plotlyDomain")
}
# add a line break if hovertext already exists
if ("hovertext" %in% names(x)) x$hovertext <- paste0(x$hovertext, "<br>")
x$hovertext <- paste0(x$hovertext, varName, ": ", forMat(x[[aesName]]))
x$hovertext <- paste0(
x$hovertext, varName, ": ", forMat(x[[paste0(aesName, "_plotlyDomain")]] %||% x[[aesName]])
)
}
x
}, data, aesMap)
Expand Down
50 changes: 47 additions & 3 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ to_basic.GeomStep <- function(data, prestats_data, layout, params, ...) {
}

#' @export
to_basic.GeomSegment <- function(data, prestats_data, layout, params, ...) {
# Every row is one segment, we convert to a line with several
# groups which can be efficiently drawn by adding NA rows.
to_basic.GeomSpoke <- function(data, prestats_data, layout, params, ...) {
data$group <- seq_len(nrow(data))
others <- data[!names(data) %in% c("x", "y", "xend", "yend")]
data <- with(data, {
Expand All @@ -187,6 +185,52 @@ to_basic.GeomSegment <- function(data, prestats_data, layout, params, ...) {
prefix_class(data, "GeomPath")
}

#' @export
to_basic.GeomSegment <- function(data, prestats_data, layout, params, ...) {
if (grid::is.unit(params$arrow$length)) {
# arrows are an extension of the line segment, and we know the arrow length
# (think r in polar coordinates), so we find the angle of the segment
# wrt to x-axis (i.e., theta in polar)
thetas <- atan2(abs(data$y - data$yend), abs(data$x - data$xend))
arrowLength <- unitConvert(params$arrow$length, "npc", "width")
lay <- tidyr::gather_(layout, "variable", "x", c("x_min", "x_max"))
# arrow length on the data scale
r <- arrowLength * diff(lay$x) * 10
# do everything in radians
arrowAngle <- (params$arrow$angle / 2) * (pi / 180)
data$x1 <- data$xend + sign(data$x - data$xend) * r * cos(thetas + arrowAngle)
data$x2 <- data$xend + sign(data$x - data$xend) * r * cos(thetas - arrowAngle)
data$y1 <- data$yend + sign(data$y - data$yend) * r * sin(thetas + arrowAngle)
data$y2 <- data$yend + sign(data$y - data$yend) * r * sin(thetas - arrowAngle)
# probably wrong
#R <- r / cos(arrowAngle / 2)
#data$xside1 <- data$xArrowBase + R * cos(arrowAngle / 2)
#data$yside1 <- data$yArrowBase + R * sin(arrowAngle / 2)
#data$xside2 <- data$xArrowBase + R * cos(-arrowAngle / 2)
#data$yside2 <- data$yArrowBase + R * sin(-arrowAngle / 2)
## TODO: group by PANEL at least!
data$group <- seq_len(nrow(data))
data$x <- NULL
data$y <- NULL
d <- tidyr::gather_(data, "variable", "x", c("x1", "x2", "xend"))
d$y <- tidyr::gather_(data, "variable", "y", c("y1", "y2", "yend"))$y
d <- d[order(d$group), ]
prefix_class(d, "GeomPolygon")
}

# Every row is one segment, we convert to a line with several
# groups which can be efficiently drawn by adding NA rows.
#browser()
#data$group <- seq_len(nrow(data))
#others <- data[!names(data) %in% c("x", "y", "xend", "yend")]
#data <- with(data, {
# rbind(cbind(x, y, others),
# cbind(x = xend, y = yend, others))
#})
#
#prefix_class(data, "GeomPath")
}

#' @export
to_basic.GeomRect <- function(data, prestats_data, layout, params, ...) {
data$group <- seq_len(nrow(data))
Expand Down