You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: r/2015-11-19-shapes.Rmd
+57Lines changed: 57 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -96,5 +96,62 @@ fig <- layout(fig, title = 'Highlighting Regions with Circles',
96
96
fig
97
97
```
98
98
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'))
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'))
Copy file name to clipboardExpand all lines: r/2018-01-29-hover-text-and-formatting.Rmd
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,30 @@ fig <- fig %>%
35
35
fig
36
36
```
37
37
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')
0 commit comments