Skip to content

Commit a9841b9

Browse files
committed
Merge pull request #488 from ropensci/fix/datehover
fix #486
2 parents 271b271 + 107ad49 commit a9841b9

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 3.2.0
3+
Version: 3.2.1
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "cpsievert1@gmail.com"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
3.2.0 -- 8 Mar 2015
1+
3.2.1 -- 10 Mar 2015
2+
3+
BUGFIX:
4+
5+
* Proper formatting for date tooltips.
6+
7+
3.2.0 -- 10 Mar 2015
28

39
CHANGES:
410

R/ggplotly.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,11 @@ gg2list <- function(p, width = NULL, height = NULL, mapping = "all", source = "A
215215
forMat <- function(x) if (is.numeric(x)) round(x, 2) else x
216216
if (aesName %in% c("x", "y")) {
217217
scaleName <- scales$get_scales(aesName)$scale_name
218-
# convert "milliseconds from the UNIX epoch" back to a date/datetime
218+
# convert "milliseconds from the UNIX epoch" to a date/datetime
219219
# http://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object-in-r
220-
if ("date" %in% scaleName) forMat <- function(x) as.Date(as.POSIXct(x / 1000, origin = "1970-01-01"))
221220
if ("datetime" %in% scaleName) forMat <- function(x) as.POSIXct(x / 1000, origin = "1970-01-01")
221+
# convert "days from the UNIX epoch" to a date/datetime
222+
if ("date" %in% scaleName) forMat <- function(x) as.Date(as.POSIXct(x * 86400, origin = "1970-01-01"))
222223
} else {
223224
if (aesName != "text") aesName <- paste0(aesName, "_plotlyDomain")
224225
}

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,49 @@ Or install the latest development version (on GitHub) via devtools:
1818
devtools::install_github("ropensci/plotly")
1919
```
2020

21-
## Introduction
21+
## Getting Started
2222

23-
If you use [ggplot2](http://cran.r-project.org/package=ggplot2), use `ggplotly()` to convert your ggplot to an interactive, web-based version!
23+
### ggplot2 converter
24+
25+
If you use [ggplot2](http://cran.r-project.org/package=ggplot2), `ggplotly()` converts your plots to an interactive, web-based version! It also provides sensible tooltips, which can help us decode values encoded as visual properties in the plot.
2426

2527
```r
2628
library(plotly)
27-
set.seed(100)
28-
d <- diamonds[sample(nrow(diamonds), 1000), ]
29-
p <- ggplot(data = d, aes(x = carat, y = price)) +
30-
geom_point(aes(text = paste("Clarity:", clarity))) +
31-
geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)
32-
(gg <- ggplotly(p))
29+
g <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
30+
stat_density_2d(aes(fill = ..level..), geom = "polygon") +
31+
xlim(1, 6) + ylim(40, 100)
32+
ggplotly(g)
3333
```
3434

35-
![https://plot.ly/~agvd/1153](http://i.imgur.com/tbKybEb.png)
35+
![https://plot.ly/~cpsievert/9836](http://imgur.com/6G4zv7b)
36+
37+
If you'd like to see how `ggplotly()` does in converting different ggplot2 examples, we host a [plotly version](http://ropensci.github.io/plotly/) of the [official ggplot2 documentation](http://docs.ggplot2.org).
3638

37-
[Click here](https://plot.ly/~agvd/1153) to interact with the resulting graph (notice the custom hover text!)
39+
### plotly's custom R interface
3840

39-
__plotly__ also supports certain chart types that ggplot2 doesn't support (such as 3D [surface](https://plot.ly/r/3d-surface-plots/), [point](https://plot.ly/r/3d-scatter-plots/), and [line](https://plot.ly/r/3d-line-plots/) plots). You can easily create these (or any other plotly) charts using the high-level interface.
41+
__plotly__ supports some chart types that ggplot2 doesn't (such as 3D [surface](https://plot.ly/r/3d-surface-plots/), [point](https://plot.ly/r/3d-scatter-plots/), and [line](https://plot.ly/r/3d-line-plots/) plots). You can create these (or any other plotly) charts using the high-level interface.
4042

4143
```r
4244
plot_ly(z = volcano, type = "surface")
4345
```
4446

4547
![https://plot.ly/~brnvg/1134](https://plot.ly/~brnvg/1134.png)
4648

47-
The `ggplotly()` function converts a ggplot object to a plotly object, so if you like, you may 'post-process' your ggplot graphs to add custom plotly features, for example:
49+
For a more detailed overview of this interface, see [here](https://cran.r-project.org/web/packages/plotly/vignettes/intro.html)
4850

49-
```r
50-
layout(gg, hovermode = "closest")
51-
```
51+
### Hooking onto plotly events
52+
53+
[plotly.js](https://github.com/plotly/plotly.js) exposes a number of 'standard' events that work consistently across plot types. It's easy to hook into these events using the `event_data()` function in shiny apps, as these examples demonstrate:
54+
55+
1. [2D events](http://104.131.111.111:3838/plotlyEvents/) ([source](https://github.com/ropensci/plotly/tree/master/inst/examples/plotlyEvents))
56+
2. [Linked Clicks](http://104.131.111.111:3838/plotlyLinkedClick/) ([source](https://github.com/ropensci/plotly/tree/master/inst/examples/plotlyLinkedClick))
57+
3. [Linked Brush](http://104.131.111.111:3838/plotlyLinkedBrush/) ([source](https://github.com/ropensci/plotly/tree/master/inst/examples/plotlyLinkedBrush))
58+
59+
60+
You can also hook into these events without shiny using `htmlwidgets::onRender()` ([example](https://github.com/ropensci/plotly/tree/master/inst/examples/onRenderHover)). This, however, requires JavaScript knowledge and makes it much harder, if not impossible, to coordinate views between htmlwidgets.
5261

5362
## Documentation
5463

55-
* [An introduction to plotly's R API](https://cran.r-project.org/web/packages/plotly/vignettes/intro.html)
5664
* Examples and vignettes on plotly's R homepage - <https://plot.ly/r>
5765
* The complete figure reference guide - <https://plot.ly/r/reference>
5866

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: "Using plotly with onRender"
3+
author: "Carson Sievert"
4+
date: "`r Sys.Date()`"
5+
output: html_document
6+
---
7+
8+
```{r, message = FALSE, warning = FALSE}
9+
library(plotly)
10+
library(htmlwidgets)
11+
plot_ly(x = 1:10, y = rnorm(10), marker = list(color = rep("black", 10))) %>%
12+
as.widget() %>%
13+
onRender('
14+
function(el, x) {
15+
var graphDiv = document.getElementById(el.id);
16+
// color this point red on hover
17+
el.on("plotly_hover", function(data) {
18+
var trace = data.points[0].curveNumber;
19+
var pt = data.points[0].pointNumber;
20+
var marker = x.data[trace].marker;
21+
marker.color[pt] = "red";
22+
Plotly.restyle(graphDiv, marker, trace)
23+
})
24+
// color this point black on unhover
25+
el.on("plotly_unhover", function(data) {
26+
var trace = data.points[0].curveNumber;
27+
var pt = data.points[0].pointNumber;
28+
var marker = x.data[trace].marker;
29+
marker.color[pt] = "black";
30+
Plotly.restyle(graphDiv, marker, trace)
31+
})
32+
}
33+
')
34+
```

0 commit comments

Comments
 (0)