-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR_icd-data.Rmd
192 lines (127 loc) · 4.34 KB
/
R_icd-data.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
title: 'R Package: icd.data'
author: "me of course"
subtitle: plotting
output: pdf_document
---
```{r}
getwd()
```
```{r}
setwd("/Users/zanedax/Documents/R_")
```
# ICD data
# import library
```{r}
library(icd.data)
```
# list the data chapters
```{r Chapters of dataset}
ls_icd_data()
```
# Plotly
```{r Plotly}
library(plotly)
fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
fig
```
# Scatterplot
```{r scatterplot}
library(plotly)
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
fig <- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', mode = 'markers',
marker = list(size = ~Gap, opacity = 0.5,
color = 'rgb(255, 65, 54)'))
fig <- fig %>% layout(title = 'Gender Gap in Earnings per University',
xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE))
fig
```
# mapping a color for continuous data variable
```{r continuous variable}
library(plotly)
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
fig <- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', mode = 'markers', color = ~Gap, colors = 'Reds',
marker = list(size = ~Gap, opacity = 0.5))
fig <- fig %>% layout(title = 'Gender Gap in Earnings per University',
xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE))
fig
```
# mapping for a categorical variable
```{r mapping categorical}
library(plotly)
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
data$State <- as.factor(c('Massachusetts', 'California', 'Massachusetts', 'Pennsylvania', 'New Jersey', 'Illinois', 'Washington DC',
'Massachusetts', 'Connecticut', 'New York', 'North Carolina', 'New Hampshire', 'New York', 'Indiana',
'New York', 'Michigan', 'Rhode Island', 'California', 'Georgia', 'California', 'California'))
fig <- plot_ly(data, x = ~Women, y = ~Men, text = ~School, type = 'scatter', mode = 'markers', size = ~Gap, color = '~State', colors = 'Paired',
marker = list(opacity = 0.5, sizemode = 'diameter'))
fig <- fig %>% layout(title = 'Gender Gap in Earnings per University',
xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE),
showlegend = FALSE)
fig
```
# text over bubbles
```{r bubble text}
library(plotly)
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")
data$State <- as.factor(c('Massachusetts', 'California', 'Massachusetts', 'Pennsylvania', 'New Jersey', 'Illinois', 'Washington DC',
'Massachusetts', 'Connecticut', 'New York', 'North Carolina', 'New Hampshire', 'New York', 'Indiana',
'New York', 'Michigan', 'Rhode Island', 'California', 'Georgia', 'California', 'California'))
fig <- plot_ly(data, x = ~Women, y = ~Men, type = 'scatter', mode = 'markers', size = ~Gap, color = ~State, colors = 'Paired',
sizes = c(10, 50),
marker = list(opacity = 0.5, sizemode = 'diameter'),
hoverinfo = 'text',
text = ~paste('School:', School, '<br>Gender Gap:', Gap))
fig <- fig %>% layout(title = 'Gender Gap in Earnings per University',
xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE),
showlegend = FALSE)
fig
```
# 2d plot
```{r 2d plot}
library(plotly)
f <- list(
family = "Courier New, monospace",
size = 28,
color = "#7f7f7f"
)
x <- list(
title = "x Axis",
titlefont = f
)
y <- list(
title = "y Axis",
titlefont = f
)
fig <- plot_ly(x = ~rnorm(10), y = ~rnorm(10), mode = "markers")
fig <- fig %>% layout(xaxis = x, yaxis = y)
fig
```
# simple bar chart
```{r bar chart}
library(plotly)
fig <- plot_ly(
x = c("giraffes", "orangutans", "monkeys"),
y = c(20, 14, 23),
name = "SF Zoo",
type = "bar",
color = "pink"
)
fig
```
# Grouped bar chart
```{r grouped bar chart}
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
fig <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo')
fig <- fig %>% add_trace(y = ~LA_Zoo, name = 'LA Zoo')
fig <- fig %>% layout(yaxis = list(title = 'Count'), barmode = 'group')
fig
```