Data Visualization In R With plotly

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@dkmathstats·
0.000 HBD
Data Visualization In R With plotly
Hi there. In this post, I showcase some plots in R with the `plotly` package. I have been using this [plotly cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) ~~as~~ and [their website for R](https://plot.ly/r/) as references.

Although I use `ggplot2` a lot in R, I think that `plotly` has some good features and the plots look pretty clean for the most part.

<center><img src="https://s3-us-west-1.amazonaws.com/plotly-tutorials/plotly-marketing-pages/images/new-branding/logo/images/plotly-logo-01-stripe%402x.png" /></center>
<center><a href="https://s3-us-west-1.amazonaws.com/plotly-tutorials/plotly-marketing-pages/images/new-branding/logo/images/plotly-logo-01-stripe%402x.png">Featured Image</a></center>



### Plots
---

* Scatterplot
* Line Plot
* Area Plot
* Bar Chart
* Histogram
* Box Plots
* 3D Scatterplot

---

To install `plotly` into R/RStudio use the code `install.packages("plotly")`. After installation, use `library(plotly)` to load in the package into R/RStudio.

The main function for plotting in `plotly` is `plot_ly()`.


**Scatterplot**

Generating a scatterplot is not too difficult. I think adding in labels and a title can be somewhat tricky as it takes time to get through some of the documentation. 

```{r}
# Scatter Plot

plot_ly(x = c(2, 3, 5, 8, 10), y = c(1, 0, 4, 2, 8), type = "scatter", mode = 'markers') %>%
  layout(xaxis = list(title = "\n x"), 
         yaxis = list(title = "y \n"),
         title = "Simple Scatterplot \n")
```

<center>![scatterplot.png](https://steemitimages.com/DQmbQq5TBmtZX7eYpjua5Wf3cBRqkcCqdM3VajSKxStrcvQ/scatterplot.png)</center>


**Line Plot**

A line plot is basically a scatterplot with a line(s) going through the points.

```{r}
# Line Plot:

plot_ly(x = c(2, 3, 5, 8, 10), y = c(1, 0, 4, 2, 8), type = "scatter", mode = "lines") %>%
  layout(xaxis = list(title = "\n x"), 
         yaxis = list(title = "y \n"),
         title = "Simple Line Plot \n")
```

<center>![linePlot.png](https://steemitimages.com/DQmUjUCZEnfexAsAKCMfAmi9UNSGTyJHerdHyWTBWVgPMxz/linePlot.png)</center>

**Area Plot**

A further extension would be adding a filled area under the line (curve).

```{r}
# Area Plot (Area Under A Line/Curve)

plot_ly(x = c(2, 3, 5, 8, 10), y = c(1, 0, 4, 2, 8), type = "scatter", mode = "lines", 
        fill = 'tozeroy')
```

<center>![areaPlot.png](https://steemitimages.com/DQmUZ5Xf76vHVcEj94LXuTwjiXPtEEMT8uz3hYw3kJCy4aM/areaPlot.png)</center>

**Bar Graph**

In the `plotly` bar graph, you need to input values for the horizontal axis and the counts in the vertical axis. I have changed the opacity to 0.5 to have the blue bars be lighter.

```{r}
# Bar Chart (Fake Survey):

plot_ly(x = c("Yes", "No"), y = c(54, 60), type = "bar", opacity = 0.5,
        marker = list(color = 'rgb(158,202,225)',
        line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
  layout(xaxis = list(title = "\n Answer"), 
         yaxis = list(title = "Counts \n"),
         title = "Bar Graph Example \n")
```

<center>![barGraph.png](https://steemitimages.com/DQmaMBZHbJ25ABGdEPEhdFbj9QG1Q31y9ivJp423XysoMv6/barGraph.png)</center>

**Histogram**

In this histogram example, I simulate/generate/sample 10000 standard normal random variables and plot the results in a histogram. The resulting histogram approximates the standard normal distribution density (bell shaped curve).

```{r}
# Histogram:

norm_rv <- rnorm(n = 10000, mean = 0, sd = 1)

plot_ly(x = norm_rv, type = "histogram") %>%
  layout(xaxis = list(title = "\n Value"), 
         yaxis = list(title = "Counts \n"),
         title = "Histogram Of Simulated Standard Normal Random Variables  \n")
```

<center>![histNorms.png](https://steemitimages.com/DQmYfJTpS7t9iuGFTGy8y6b2jzcsghcJejFK22u576dLhdN/histNorms.png)</center>

**Box Plots**

```{r}
# Box Plot:

plot_ly(y = rnorm(100), type = "box")
```

<center>![boxPlot01.png](https://steemitimages.com/DQmbkFBJqwY4FxmMvn6yZzgAQ3rVSKoKrHu9jcZRXdDb1AE/boxPlot01.png)</center>

The `add_trace()` function allows for an additional box plot. The second box plot is for chi-squared random variables. (A chi-squared random variable is the square of a normal random variable.)

```{r}
plot_ly(y = rnorm(100), type = "box") %>%
  add_trace(y = rchisq(n = 100, df = 1, ncp = 0)) # Two box plots
```
<center>![boxPlot02.png](https://steemitimages.com/DQmUMLAH2noqqLmZMDy47EFmd1b9Jm7pVoobk5H352kjmM2/boxPlot02.png)
</center>

**3D Scatterplot**

You can create three dimensional scatter plots in `plotly` by having the type as `scatter3d` and having `x`, `y` and `z`. In my computer and RStudio, I found it hard to play with the 3D output. The image below could be better.

```{r}
# 3D Scatter Plot:

plot_ly(x = rnorm(10), y = rnorm(10), z = rnorm(10),
        type = "scatter3d",
        mode = "markers")
```

<center>![scatterplot3d.png](https://steemitimages.com/DQmc6GT5ssqxXPzcrLQnRkpeCqmrw32qJMXMYH1yuDkJZB4/scatterplot3d.png)</center>

---

Edit: Fixed a few typos.
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,