-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.script_plotly.R
More file actions
33 lines (27 loc) · 1.33 KB
/
7.script_plotly.R
File metadata and controls
33 lines (27 loc) · 1.33 KB
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
## Load libraries
library(plotly)
library(ggpubr)
## Load data
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/violin_data.csv")
## Basic violin plot
plot_ly(df, y = ~total_bill, type = 'violin', color = "pink", box = list(visible = T),
meanline = list(visible = T), x0 = 'Total bill') %>%
layout(yaxis = list(title = "", zeroline = F))
## Plot by sex
plot_ly(df, x = ~sex, y = ~total_bill, split = ~sex, type = 'violin', box = list(visible = T),
meanline = list(visible = T), x0 = 'Total bill') %>%
layout(yaxis = list(title = "Sex", zeroline = F))
## Plot by sex and day
plot_ly(df, type = 'violin') %>%
add_trace(x = ~day[df$sex == 'Male'], y = ~total_bill[df$sex == 'Male'],
legendgroup = 'M', scalegroup = 'M', name = 'M',
box = list(visible = T),
meanline = list(visible = T),
color = I('red')) %>%
add_trace(x = ~day[df$sex == 'Female'], y = ~total_bill[df$sex == 'Female'],
legendgroup = 'F', scalegroup = 'F', name = 'F',
box = list(visible = T),
meanline = list(visible = T),
color = I('yellow')) %>%
layout(yaxis = list(zeroline = F, title = 'Total bill'), violinmode = 'group',
xaxis = list(title = ''))