|
1 | 1 | ---
|
| 2 | +name: Hover Text and Formatting |
| 3 | +permalink: r/hover-text-and-formatting/ |
2 | 4 | description: How to use hover text and formatting in R with Plotly.
|
3 |
| -display_as: file_settings |
4 |
| -language: r |
5 | 5 | layout: base
|
6 |
| -name: Hover Text and Formatting |
| 6 | +thumbnail: thumbnail/hover-text.jpg |
| 7 | +language: r |
| 8 | +display_as: file_settings |
7 | 9 | order: 7
|
| 10 | +page_type: u-guide |
8 | 11 | output:
|
9 | 12 | html_document:
|
10 | 13 | keep_md: true
|
11 |
| -page_type: u-guide |
12 |
| -permalink: r/hover-text-and-formatting/ |
13 |
| -thumbnail: thumbnail/hover-text.png |
14 | 14 | ---
|
15 | 15 |
|
16 | 16 | ```{r, echo = FALSE, message=FALSE}
|
@@ -66,22 +66,20 @@ p <- plot_ly(type = 'scatter', mode = 'markers') %>%
|
66 | 66 | showlegend = F
|
67 | 67 | ) %>%
|
68 | 68 | layout(
|
69 |
| - title = "Set hover text formatting<br><a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>", |
70 |
| - titlefont = list( |
71 |
| - size = 10 |
72 |
| - ), |
73 |
| - xaxis = list( |
74 |
| - zeroline = F |
75 |
| - ), |
76 |
| - yaxis = list( |
77 |
| - hoverformat = '.2f' |
78 |
| - ) |
79 |
| - ) |
| 69 | + title = list(text="Set hover text formatting<br><a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>", |
| 70 | + size = 10), |
| 71 | + xaxis = list(zeroline = F), |
| 72 | + yaxis = list(hoverformat = '.2f')) |
80 | 73 |
|
81 | 74 | p
|
82 | 75 | ```
|
83 | 76 |
|
84 |
| -#### Hovertemplate |
| 77 | +#### Customize Tooltip Text with a Hovertemplate |
| 78 | + |
| 79 | +To customize the tooltip on your graph you can use [hovertemplate](https://plot.ly/r/reference/#pie-hovertemplate), which is a template string used for rendering the information that appear on hoverbox. |
| 80 | +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). |
| 81 | +Hovertemplate customize the tooltip text vs. [texttemplate](https://plot.ly/r/reference/#pie-texttemplate) which customizes the text that appears on your chart. <br> |
| 82 | +Set the horizontal alignment of the text within tooltip with [hoverlabel.align](https://plot.ly/r/reference/#layout-hoverlabel-align). |
85 | 83 |
|
86 | 84 | ```{r}
|
87 | 85 | library(plotly)
|
@@ -110,6 +108,21 @@ p <- plot_ly() %>%
|
110 | 108 | p
|
111 | 109 | ```
|
112 | 110 |
|
| 111 | +```{r} |
| 112 | +library(plotly) |
| 113 | +p <- plot_ly() %>% |
| 114 | + add_trace( |
| 115 | + type = "pie", |
| 116 | + name = "", |
| 117 | + values = c(2, 1, 3, 2.5), |
| 118 | + labels = c("R", "Python", "Java Script", "Matlab"), |
| 119 | + text = c("textA", "TextB", "TextC", "TextD"), |
| 120 | + hovertemplate = "%{label}: <br>Popularity: %{percent} </br> %{text}") |
| 121 | +
|
| 122 | +
|
| 123 | +p |
| 124 | +``` |
| 125 | + |
113 | 126 | #### Advanced Hovertemplate
|
114 | 127 |
|
115 | 128 | ```{r}
|
|
141 | 154 |
|
142 | 155 | #### Reference
|
143 | 156 |
|
144 |
| -See [https://plot.ly/r/reference/](https://plot.ly/r/reference/) for more information and options! |
| 157 | +See [https://plot.ly/r/reference/](https://plot.ly/r/reference/) for more information and options! |
0 commit comments