-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-hover-text-and-formatting.Rmd
61 lines (48 loc) · 1.47 KB
/
2021-08-04-hover-text-and-formatting.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
name: Hover Text and Formatting
permalink: ggplot2/hover-text-and-formatting/
description: How to use hover text and formatting in ggplot2 with Plotly.
layout: base
thumnail_github: hover-text.png
language: ggplot2
page_type: u-guide
display_as: base
order: 23
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
### Maps
```{r}
library(plotly)
data(canada.cities, package="maps")
p <- ggplot(canada.cities, aes(long, lat)) +
borders(regions="canada", name="borders") +
coord_equal() +
geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
ggplotly(p)
```
### Custom Tooltip
```{r}
library(plotly)
#install.packages("gapminder")
library(gapminder)
p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, text = paste("country:", country))) +
geom_point(alpha = (1/3)) + scale_x_log10()
ggplotly(p)
```
Inspired by <a href="https://github.com/jennybc/ggplot2-tutorial/blob/master/gapminder-ggplot2-scatterplot.md">Gapminder Tutorial</a>
### Control Events
```{r}
library(plotly)
#install.packages("gapminder")
library(gapminder)
p <- ggplot(gapminder, aes(x = year, y = lifeExp, text = paste("country:",country))) +
geom_point() +
facet_wrap(~ continent)
ggplotly(p)
```
Inspired by <a href="https://github.com/jennybc/ggplot2-tutorial/blob/master/gapminder-ggplot2-scatterplot.md">Gapminder Tutorial</a>