create account

Data Visualization In R With plotly by dkmathstats

View this thread on: hive.blogpeakd.comecency.com
· @dkmathstats · (edited)
$113.27
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.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authordkmathstats
permlinkdata-visualization-in-r-with-plotly
categoryprogramming
json_metadata{"tags":["programming","r","data","science","technology"],"image":["https://s3-us-west-1.amazonaws.com/plotly-tutorials/plotly-marketing-pages/images/new-branding/logo/images/plotly-logo-01-stripe%402x.png","https://steemitimages.com/DQmbQq5TBmtZX7eYpjua5Wf3cBRqkcCqdM3VajSKxStrcvQ/scatterplot.png","https://steemitimages.com/DQmUjUCZEnfexAsAKCMfAmi9UNSGTyJHerdHyWTBWVgPMxz/linePlot.png","https://steemitimages.com/DQmUZ5Xf76vHVcEj94LXuTwjiXPtEEMT8uz3hYw3kJCy4aM/areaPlot.png","https://steemitimages.com/DQmaMBZHbJ25ABGdEPEhdFbj9QG1Q31y9ivJp423XysoMv6/barGraph.png","https://steemitimages.com/DQmYfJTpS7t9iuGFTGy8y6b2jzcsghcJejFK22u576dLhdN/histNorms.png","https://steemitimages.com/DQmbkFBJqwY4FxmMvn6yZzgAQ3rVSKoKrHu9jcZRXdDb1AE/boxPlot01.png","https://steemitimages.com/DQmUMLAH2noqqLmZMDy47EFmd1b9Jm7pVoobk5H352kjmM2/boxPlot02.png","https://steemitimages.com/DQmc6GT5ssqxXPzcrLQnRkpeCqmrw32qJMXMYH1yuDkJZB4/scatterplot3d.png"],"links":["https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf","https://plot.ly/r/","https://s3-us-west-1.amazonaws.com/plotly-tutorials/plotly-marketing-pages/images/new-branding/logo/images/plotly-logo-01-stripe%402x.png"],"app":"steemit/0.1","format":"markdown"}
created2017-08-23 15:58:42
last_update2017-08-24 03:23:18
depth0
children6
last_payout2017-08-30 15:58:42
cashout_time1969-12-31 23:59:59
total_payout_value85.040 HBD
curator_payout_value28.227 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,989
author_reputation150,278,811,385,224
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,650,733
net_rshares27,787,705,174,647
author_curate_reward""
vote details (46)
@anos ·
please upvote to me https://steemit.com/food/@anos/ice-cream-steak-with-strawberry and follow me
properties (22)
authoranos
permlinkre-dkmathstats-data-visualization-in-r-with-plotly-20170823t160044293z
categoryprogramming
json_metadata{"tags":["programming"],"links":["https://steemit.com/food/@anos/ice-cream-steak-with-strawberry"],"app":"steemit/0.1"}
created2017-08-23 16:00:48
last_update2017-08-23 16:00:48
depth1
children0
last_payout2017-08-30 16:00:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length96
author_reputation-28,514,146,177
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,650,927
net_rshares0
@bitrocker2020 ·
i was just starting out on ploty, have not gotten the hang of it yet. great technical blogs. are you developing anything on top of steem block-chain ?
👍  
properties (23)
authorbitrocker2020
permlinkre-dkmathstats-data-visualization-in-r-with-plotly-20170824t005627138z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-08-24 00:56:27
last_update2017-08-24 00:56:27
depth1
children1
last_payout2017-08-31 00:56:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length150
author_reputation974,750,896,734,572
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,689,878
net_rshares3,921,670,155
author_curate_reward""
vote details (1)
@dkmathstats ·
With `plotly` and other data visualization tools in R and Python, there is a lot of syntax involved and it can be confusing. I would say to start with the plotly cheatsheet link above and their documentation.

Are you using R  or Python?

> are you developing anything on top of steem block-chain ?

No I am not.
properties (22)
authordkmathstats
permlinkre-bitrocker2020-re-dkmathstats-data-visualization-in-r-with-plotly-20170824t010445148z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-08-24 01:04:42
last_update2017-08-24 01:04:42
depth2
children0
last_payout2017-08-31 01:04:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length312
author_reputation150,278,811,385,224
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,690,368
net_rshares0
@runtime ·
$0.03
Good intro-post.  I'm interested in what kind of jobs I could get to make use of the data visualisation techniques I've been learning. Are you doing this kind of stuff professionally, or just learning it for enjoyment?

Thanks!
👍  
properties (23)
authorruntime
permlinkre-dkmathstats-data-visualization-in-r-with-plotly-20170827t094140142z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-08-27 09:41:36
last_update2017-08-27 09:41:36
depth1
children1
last_payout2017-09-03 09:41:36
cashout_time1969-12-31 23:59:59
total_payout_value0.021 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length227
author_reputation108,185,118,203
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,010,610
net_rshares7,327,079,653
author_curate_reward""
vote details (1)
@dkmathstats ·
Thank you for your comment @runtime.

> Are you doing this kind of stuff professionally, or just learning it for enjoyment?

For enjoyment and for employment into stats and/or data science.

> I'm interested in what kind of jobs I could get to make use of the data visualisation techniques I've been learning. 

I think there is good demand for data visualization skills but I can't say for sure.  Data visualization can be supplemented with skills in data cleaning, data extraction and data analysis (with prediction).
properties (22)
authordkmathstats
permlinkre-runtime-re-dkmathstats-data-visualization-in-r-with-plotly-20170827t140313677z
categoryprogramming
json_metadata{"tags":["programming"],"users":["runtime"],"app":"steemit/0.1"}
created2017-08-27 14:03:12
last_update2017-08-27 14:03:12
depth2
children0
last_payout2017-09-03 14:03:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length519
author_reputation150,278,811,385,224
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id13,027,752
net_rshares0
@saadhashmi ·
@dkmathstats  welcome to steemit community very nice post have a good day ahead thankyou
properties (22)
authorsaadhashmi
permlinkre-dkmathstats-data-visualization-in-r-with-plotly-20170826t201445349z
categoryprogramming
json_metadata{"tags":["programming"],"users":["dkmathstats"],"app":"steemit/0.1"}
created2017-08-26 20:14:42
last_update2017-08-26 20:14:42
depth1
children0
last_payout2017-09-02 20:14:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length88
author_reputation29,360,148,946
root_title"Data Visualization In R With plotly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,965,283
net_rshares0