Skip to content

Commit fe712e2

Browse files
author
Joseph Damiba
committed
update docs for new version of package
1 parent b05ae49 commit fe712e2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

r/2015-11-19-shapes.Rmd

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,62 @@ fig <- layout(fig, title = 'Highlighting Regions with Circles',
9696
fig
9797
```
9898

99+
### Drawing Shapes on Cartesian Plots
100+
101+
You can create layout shapes programatically, but you can also draw shapes manually by setting the `dragmode` to one of the shape-drawing modes: `drawline`,`drawopenpath`, `drawclosedpath`, `drawcircle`, or `drawrect`. If you need to switch between different shape-drawing or other dragmodes (panning, selecting, etc.), modebar buttons can be added in the `config` of your figure to select the dragmode.
102+
103+
If you switch to a different dragmode such as pan or zoom, you will need to select the drawing tool in the modebar to go back to shape drawing.
104+
105+
This shape-drawing feature is particularly interesting for annotating graphs, in particular [image traces](https://plotly.com/r/displaying-images/).
106+
107+
Once you have drawn shapes, you can select and modify an existing shape by clicking on its boundary (note the arrow pointer). Its fillcolor turns to pink to highlight the activated shape and then you can
108+
109+
- drag and resize it for lines, rectangles and circles/ellipses
110+
- drag and move individual vertices for closed paths
111+
- move individual vertices for open paths.
112+
113+
An activated shape is deleted by cliking on the `eraseshape` button.
114+
115+
Drawing or modifying a shape triggers a `relayout` event, which [can be captured by a callback inside a Dash For R application](https://dash.plotly.com/interactive-graphing).
116+
117+
```{r}
118+
library(plotly)
119+
120+
df <- diamonds[sample(nrow(diamonds), 1000), ]
121+
122+
fig <- plot_ly(df, x = ~carat, y = ~price, text = ~paste("Clarity: ", clarity),
123+
mode = "markers", color = ~carat, size = ~carat)
124+
125+
fig <- layout(fig, dragmode="drawrect", xaxis = list(title = 'Click and drag inside the figure to draw a rectangle or select another shape in the modebar'))
126+
127+
fig <- fig %>%
128+
config(modeBarButtonsToAdd = list("drawine", "drawopenpath", "drawclosedpath", "drawcircle", "drawrect", "eraseshape" ) )
129+
130+
fig
131+
132+
```
133+
134+
### Style of user-drawn shapes
135+
136+
The layout `newshape` attribute controls the visual appearance of new shapes drawn by the user. `newshape` attributes have the same names as layout shapes.
137+
138+
Note on shape opacity: having a new shape's opacity > 0.5 makes it possible to activate a shape by clicking inside the shape (for opacity <= 0.5 you have to click on the border of the shape), but you cannot start a new shape within an existing shape (which is possible for an opacity <= 0.5).
139+
140+
```{r}
141+
library(plotly)
142+
143+
df <- diamonds[sample(nrow(diamonds), 1000), ]
144+
145+
fig <- plot_ly(df, x = ~carat, y = ~price, text = ~paste("Clarity: ", clarity),
146+
mode = "markers", color = ~carat, size = ~carat)
147+
148+
fig <- layout(fig, dragmode="drawrect", newshape=list(fillcolor="yellow", opacity=0.5), xaxis = list(title = 'Click and drag inside the figure to draw a rectangle or select another shape in the modebar'))
149+
150+
fig <- fig %>%
151+
config(modeBarButtonsToAdd = list("drawine", "drawopenpath", "drawclosedpath", "drawcircle", "drawrect", "eraseshape" ) )
152+
153+
fig
154+
155+
```
99156
### Reference
100157
Check out our <b>[reference page](https://plotly.com/r/reference/#layout-shapes)</b> for more information on using shapes!

r/2018-01-29-hover-text-and-formatting.Rmd

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ fig <- fig %>%
3535
fig
3636
```
3737

38+
#### Unified hovermode
39+
40+
If you set the `hovermode` attribute of your figure's layout to `x unified` or `y unified`), a single hover label will appear, describing one point per trace, for points at the same x (or y) value as the cursor.
41+
42+
If multiple points in a given trace exist at the same coordinate, only one will get an entry in the hover label. In the line plot below we have forced markers to appear, to make it clearer what can be hovered over:
43+
44+
```{r}
45+
library(plotly)
46+
47+
trace_0 <- rnorm(100, mean = 5)
48+
trace_1 <- rnorm(100, mean = 0)
49+
trace_2 <- rnorm(100, mean = -5)
50+
x <- c(1:100)
51+
52+
data <- data.frame(x, trace_0, trace_1, trace_2)
53+
54+
fig <- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines')
55+
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')
56+
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
57+
fig <- fig %>%
58+
layout(hovermode = "x unified")
59+
fig
60+
```
61+
3862
#### Format Hover Text
3963

4064
```{r}

0 commit comments

Comments
 (0)