# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
install.packages("data.table")
}if (!requireNamespace("jsonlite", quietly = TRUE)) {
install.packages("jsonlite")
}if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
# Load packages
library(data.table)
library(jsonlite)
library(ggplot2)
Pareto Chart
Note
Hiplot website
This page is the tutorial for source code version of the Hiplot Pareto Chart
plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
data.table
;jsonlite
;ggplot2
Data Preparation
The case data represents the sales data of a product on multiple platforms. The plugin will automatically plot a bar chart with sales data in descending order and simultaneously calculate the cumulative sales to draw the cumulative line chart.
# Load data
<- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/pareto-chart/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
data
# Convert data structure
<- data[order(-data[["sales"]]), ]
data "channel"]] <- factor(data[["channel"]], levels = data[["channel"]])
data[[## Calculate percentage number
$accumulating <- cumsum(data[["sales"]])
data<- max(data[["sales"]])
max_y <- sum(data[["sales"]]) / max_y
cal_num $accumulating <- data$accumulating / cal_num
data
# View data
head(data)
channel sales accumulating
2 JD 500 132.9787
5 Tmall 400 239.3617
4 Pinduoduo 300 319.1489
3 Amazon 230 380.3191
6 Shopee 200 433.5106
1 TaoBao 100 460.1064
Visualization
# Pareto Chart
<- ggplot(data, aes(x = channel, y = sales, fill = channel)) +
p geom_bar(stat = "identity") +
geom_line(aes(y = accumulating), group = 1) +
geom_point(aes(y = accumulating), show.legend = FALSE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . / max_y * 100, name = "Percentage")) +
scale_fill_manual(values = c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF",
"#F39B7FFF","#8491B4FF","#91D1C2FF","#DC0000FF")) +
theme_bw()
p
