iris data. (From the datasets package, loaded by default when you start R.)This is data on 150 irises. Variables recorded are:
Here’s the code.
plot_ly(data = iris, x = ~Sepal.Length,
y = ~Petal.Length,
color = ~Species,
text = ~paste("Sepal Width: ", Sepal.Width,
'<br>Petal Width:', Petal.Width))You can see the Grammar of Graphics at work, especially in the aesthetic mappings. Note also the use of the HTML tag <br>.
Here’s the code:
p <-
iris %>%
ggplot(aes(x = Sepal.Length,
y = Petal.Length)) +
geom_point(aes(color = Species))
plotly::ggplotly(p)But quite often the plot is a bit distorted, as you can see in this case. Hence when you want interactive graphics for production (final version of your report) rather than for preliminary exploration, you should switch from ggplot to a package like plotly.
Here’s how to do it:
plot_ly(iris, x = ~Sepal.Length,
y = ~Petal.Length,
z = ~Petal.Width,
color = ~Species,
text = ~paste("Sepal Width: ",
Sepal.Width)) %>%
add_markers() %>%
layout(scene = list(
xaxis = list(title = 'Sepal Length'),
yaxis = list(title = 'Petal Length'),
zaxis = list(title = 'Petal Width')))Here’s the code (BabyNames is from the DataComputing package).
BabyNames %>%
filter(name %in% c("Mary", "Maria")) %>%
group_by(year, name) %>%
summarise(popularity = sum(count)) %>%
hchart("line", hcaes(x = year,
y = popularity,
group = name))Note that highcharter is not free for commercial or governmental use.
Data Visualization as a Career: For much more inspiring examples of data visualization, and for advice on pursuing data vis as a career or as a skill-set in your career, consult Jim Vallandingham’s blog post:
---
title: "More About Data Visualization"
author: "Homer White"
date: "9/14/2017"
output:
flexdashboard::flex_dashboard:
source_code: embed
storyboard: true
---
```{r include = FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(tidyverse)
data("BabyNames", package = "DataComputing")
library(plotly)
library(highcharter)
```
### Have a look at the `iris` data. (From the **datasets** package, loaded by default when you start R.){data-commentary-width=450}
```{r echo = FALSE}
DT::datatable(iris, options = list(
pageLength = 8
))
```
***
This is data on 150 irises. Variables recorded are:
* Sepal Length
* Sepal Width
* Petal Length
* Petal Width
* Species:
* setosa
* versicolor
* virginica
### The **plotly** package uses Javascript to add interactivity to a plot. {data-commentary-width=550}
```{r echo = FALSE}
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length,
color = ~Species,
text = ~paste("Sepal Width: ", Sepal.Width,
'
Petal Width:', Petal.Width))
```
***
Here's the code.
```{r eval = FALSE}
plot_ly(data = iris, x = ~Sepal.Length,
y = ~Petal.Length,
color = ~Species,
text = ~paste("Sepal Width: ", Sepal.Width,
'
Petal Width:', Petal.Width))
```
You can see the Grammar of Graphics at work, especially in the aesthetic mappings. Note also the use of the HTML tag `
`.
### Plotly can attempt to make a ggplot interactive. {data-commentary-width=500}
```{r echo = FALSE}
p <-
iris %>%
ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(aes(color = Species))
plotly::ggplotly(p)
```
***
Here's the code:
```{r eval = FALSE}
p <-
iris %>%
ggplot(aes(x = Sepal.Length,
y = Petal.Length)) +
geom_point(aes(color = Species))
plotly::ggplotly(p)
```
But quite often the plot is a bit distorted, as you can see in this case. Hence when you want interactive graphics for *production* (final version of your report) rather than for preliminary exploration, you should switch from **ggplot** to a package like **plotly**.
### Plotly has the capacity to produce 3D plots. {data-commentary-width=450}
```{r echo = FALSE}
plot_ly(iris, x = ~Sepal.Length, y = ~Petal.Length,
z = ~Petal.Width, color = ~Species,
text = ~paste("Sepal Width: ", Sepal.Width)) %>%
add_markers() %>%
layout(scene = list(xaxis = list(title = 'Sepal Length'),
yaxis = list(title = 'Petal Length'),
zaxis = list(title = 'Petal Width')))
```
***
Here's how to do it:
```{r eval = FALSE}
plot_ly(iris, x = ~Sepal.Length,
y = ~Petal.Length,
z = ~Petal.Width,
color = ~Species,
text = ~paste("Sepal Width: ",
Sepal.Width)) %>%
add_markers() %>%
layout(scene = list(
xaxis = list(title = 'Sepal Length'),
yaxis = list(title = 'Petal Length'),
zaxis = list(title = 'Petal Width')))
```
### Another popular interactive graphing package is **highcharter**. {data-commentary-width=450}
```{r echo = F}
BabyNames %>%
filter(name %in% c("Mary", "Maria")) %>%
group_by(year, name) %>%
summarise(popularity = sum(count)) %>%
hchart("line", hcaes(x = year,
y = popularity,
group = name))
```
***
Here's the code (`BabyNames` is from the **DataComputing** package).
```{r eval = F}
BabyNames %>%
filter(name %in% c("Mary", "Maria")) %>%
group_by(year, name) %>%
summarise(popularity = sum(count)) %>%
hchart("line", hcaes(x = year,
y = popularity,
group = name))
```
Note that **highcharter** is not free for commercial or governmental use.
**Data Visualization as a Career**: For much more inspiring examples of data visualization, and for advice on pursuing data vis as a career or as a skill-set in your career, consult Jim Vallandingham's blog post:
>[Becoming a Data Visualization Practitioner](http://vallandingham.me/becoming_datavis_practitioner.html)