Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9ade1e

Browse files
author
mahdis-z
committed
text template
1 parent 5bee541 commit e9ade1e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

r/2015-07-30-text-and-annotations.Rmd

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,73 @@ fig <- fig %>% add_annotations(
365365
366366
fig
367367
```
368+
### Customize Displayed Text with a Text Template
368369

370+
To show an arbitrary text in your chart you can use [texttemplate](https://plot.ly/r/reference/#pie-texttemplate), which is a template string used for rendering the information, and will override [textinfo](https://plot.ly/r/reference/#treemap-textinfo).
371+
This template string can include `variables` in %{variable} format, `numbers` in [d3-format's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_forma), and `date` in [d3-time-fomrat's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Formatting.md#format).
372+
`texttemplate` customizes the text that appears on your plot vs. [hovertemplate](https://plot.ly/r/reference/#pie-hovertemplate) that customizes the tooltip text.
373+
374+
```{r}
375+
library(plotly)
376+
377+
fig <- plot_ly(
378+
type='pie',
379+
values=c(40000000, 20000000, 30000000, 10000000),
380+
labels=c("Wages", "Operating expenses", "Cost of sales", "Insurance"),
381+
texttemplate="%{label}: %{value:$,s} <br>(%{percent})",
382+
textposition="inside")
383+
fig
384+
```
385+
386+
### Customize Text Template
387+
388+
The following example uses [textfont](https://plot.ly/r/reference/#scatterternary-textfont) to customize the added text.
389+
390+
```{r}
391+
library(plotly)
392+
393+
fig <- plot_ly(
394+
type='scatterternary',
395+
a = c(3, 2, 5),
396+
b = c(2, 5, 2),
397+
c = c(5, 2, 2),
398+
mode = "markers+text",
399+
text = c("A", "B", "C"),
400+
texttemplate = "%{text}<br>(%{a:.2f}, %{b:.2f}, %{c:.2f})",
401+
textposition = "bottom center",
402+
textfont = list(family= "Times", size= c(18, 21, 20), color= c("IndianRed", "MediumPurple", "DarkOrange"))
403+
)
404+
fig
405+
```
406+
### Set Date in Text Template
407+
408+
The following example shows how to show date by setting [axis.type](https://plot.ly/r/reference/#layout-yaxis-type) in [funnel charts](https://plot.ly/r/funnel-charts/).
409+
As you can see [textinfo](https://plot.ly/r/reference/#funnel-textinfo) and [texttemplate](https://plot.ly/r/reference/#funnel-texttemplate) have the same functionality when you want to determine 'just' the trace information on the graph.
410+
411+
```{r}
412+
fig <- plot_ly()
413+
414+
fig <- fig %>% add_trace(
415+
type='funnel',
416+
name = 'Montreal',
417+
orientation = "h",
418+
y = c("2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"),
419+
x = c(100, 60, 40, 20),
420+
textposition = "inside",
421+
texttemplate = "%{y| %a. %_d %b %Y}")
422+
423+
fig <- fig %>% add_trace(
424+
type='funnel',
425+
name = 'Vancouver',
426+
orientation = "h",
427+
y = c("2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"),
428+
x = c(90, 70, 50, 10),
429+
textposition = "inside",
430+
textinfo = "label")
431+
432+
fig <- fig %>% layout(yaxis = list(type= 'date'))
433+
fig
434+
```
369435
### Set Annotation Coordinate References
370436

371437
```{r}

0 commit comments

Comments
 (0)