-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive plotly in r + start R.Rmd
119 lines (58 loc) · 1.83 KB
/
interactive plotly in r + start R.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
---
title: "Interactive visualization with pltly in R +
use of pacman to start R coding"
author: "Dimo - Wins with Data"
date: "`r Sys.Date()`"
output: html_document
---
* Load and install required libraries automatically if needed
```{r setup}
# instal pacman package if needed
if (!require("pacman")) install.packages("pacman")
# now load and install other packages for your script
pacman::p_load(tidyverse,plotly,
dplyr)
```
* use a built-in r dataset for practice
```{r}
#define the built in dataset as an r object for ease of use
df = mtcars
head(df)
```
```{r}
# convert rownames to a column
df$categories = rownames(df)
```
* interactive bar chart with plotly r: Value of mpg variable over different Categories
```{r}
fig <- plot_ly(df,
x = ~categories, y = ~mpg,
type = 'bar', name= 'mpg variable'
)
fig1 <- fig %>%
layout(title = "mtcar data explorations 1")
fig1
```
* order bar chart: interactive chart & order on x axis category factors
```{r}
fig <- plot_ly(df,
x = ~categories, y = ~mpg,
type = 'bar', name= 'mpg variable'
)
fig2 <- fig %>% layout(title = "mtcars data exploration",
xaxis = list(title = "",
categoryorder = "total descending"),
yaxis = list(title = ""))
fig2
```
* two bar charts in one: interactive bar chart, multiple categories
```{r,echo=FALSE}
fig <- plot_ly(df,
x = ~categories, y = ~mpg,
type = 'bar', name= 'mpg variable'
)
fig3 <- fig %>%
layout(title = "mtcar data explorations") %>%
add_trace(y = ~cyl, name = 'cyl variable')
fig3
```