-
Notifications
You must be signed in to change notification settings - Fork 0
/
Making Boxplots.Rmd
76 lines (58 loc) · 2.17 KB
/
Making Boxplots.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
---
title: "How to make a boxplot using R"
author: "R-Ladies Tunis"
date: "25/06/2020"
output:
prettydoc::html_pretty:
theme: hpstr
highlight: github
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# **Introduction**
Previously, we talked about barplots and how to make them. In this article we will talk about boxplots. A boxplot is a standardized way of displaying the distribution of data based on a five number summary (“minimum”, first quartile (Q1), median, third quartile (Q3), and “maximum”). It can tell you about your outliers and what their values are. It can also tell you if your data is symmetrical, how tightly your data is grouped, and if and how your data is skewed.
# **Basic Boxplot**
First, we will create a basic boxplot:
```{r,include=FALSE}
library(ggplot2)
```
```{r,include=TRUE}
# Boxplot of Diamonds'price by cut
boxplot(price~cut,data=diamonds, main="Dimonds'price by cut",
xlab="Price", ylab="Cut")
```
# **Boxplot using ggplot2**
Now, let's make the graph look prettier using ggplot2. The geom_boxplot() function we can determine the color of the bars. With ggtitle() you can add the title of the graph and the title of the axes.
```{r,include=TRUE}
#loding ggplot2 package
library(ggplot2)
p<-ggplot(diamonds, aes(x=cut, y=price, color=cut)) +
geom_boxplot()+
ggtitle("Diamonds'price by cut") +
xlab("Price") + ylab("Cut")
p
```
# **Boxplot using ggstatplot**
ggstatsplot is an extension of ggplot2 package for creating graphics with details from statistical tests included in the information-rich plots themselves. We will use this package to create a boxplot.
```{r,include=TRUE,message=FALSE,warning=FALSE}
# loading needed libraries
library(ggstatsplot)
# for reproducibility
set.seed(123)
# plot
ggstatsplot::ggbetweenstats(
data = diamonds,
x = cut,
y = price,
title = "Diamonds'price by cut"
)
```
# **Boxplot using plotly**
Now let's make the barplot interactive using plotly. An interactive graph is very useful when we work with dashboards.
```{r,include=TRUE,message=FALSE,warning=FALSE}
#loding plotly package
library(plotly)
p <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")
p
```