I have a projects list with dynamic KPIs per project, each KPI has (a value and target)
I am receiving the dataset in the below format, I am trying to drow one chart for kpi for one project :
dataset <- data.frame(
value = c(3,5,200.....),
Target = c(10,20,250.....),
KPI = c("a","b","c","d",....)
)
Is there any way to achieve multi-donut KPIs using ggplot!! (or even a pie chart) to look similar to the below image!
CodePudding user response:
You could do:
library(tidyverse)
dataset %>%
mutate(perc = value/Target) %>%
ggplot(aes(x = 3, y = perc))
geom_linerange(aes(ymin = 0, ymax = 1), size = 4, color = "#caeee3")
geom_linerange(aes(ymin = 0, ymax = perc), size = 4, color = "#01b8aa")
geom_text(aes(x = 1.5, y = 0, label = scales::percent(perc)), size = 6)
geom_text(aes(x = 0, y = 0, label = KPI), size = 8, color = 'gray80')
coord_polar(theta = 'y')
theme_void()
scale_x_continuous(limits = c(0, 4), expand = c(0, 0))
facet_wrap(.~KPI)
theme(strip.text = element_blank())
CodePudding user response:
Proposition
##### Libraries
library(ggplot2)
library(ggpubr)
##### Data management
# KPI a
dataset_a <- data.frame(count=c(3, 7),
KPI=c("a", "a"),
category=c("value", "nonReached"))
dataset_a$fraction <- prop.table(dataset_a$count)
dataset_a$ymax <- cumsum(dataset_a$fraction)
dataset_a$ymin <- c(0, head(dataset_a$ymax, n=-1))
dataset_a$labelPosition <- (dataset_a$ymax dataset_a$ymin) / 2
dataset_a$label <- paste0(dataset_a$category, "\n value: ", dataset_a$count)
# KPI b
dataset_b <- data.frame(count=c(5, 15),
KPI=c("b", "b"),
category=c("value", "nonReached"))
dataset_b$fraction <- prop.table(dataset_b$count)
dataset_b$ymax <- cumsum(dataset_b$fraction)
dataset_b$ymin <- c(0, head(dataset_b$ymax, n=-1))
dataset_b$labelPosition <- (dataset_b$ymax dataset_b$ymin) / 2
dataset_b$label <- paste0(dataset_b$category, "\n value: ", dataset_b$count)
# KPI c
dataset_c <- data.frame(count=c(200, 50),
KPI=c("c", "c"),
category=c("value", "nonReached"))
dataset_c$fraction <- prop.table(dataset_c$count)
dataset_c$ymax <- cumsum(dataset_c$fraction)
dataset_c$ymin <- c(0, head(dataset_c$ymax, n=-1))
dataset_c$labelPosition <- (dataset_c$ymax dataset_c$ymin) / 2
dataset_c$label <- paste0(dataset_c$category, "\n value: ", dataset_c$count)
##### The plots
pie_a <- ggplot(dataset_a, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category))
geom_rect()
scale_fill_manual(values=c("#2bbd97", "#64f0cb"))
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
geom_text(x=0.5, y=2, label=paste(round(subset(dataset_a, category=="value")$fraction, 2)*100, "%", sep=""), size=9)
geom_text(x=-1.5, y=2, label=paste("KPI a"), size=6, color="#a4a4a4")
pie_b <- ggplot(dataset_b, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category))
geom_rect()
scale_fill_manual(values=c("#2bbd97", "#64f0cb"))
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
geom_text(x=0.5, y=2, label=paste(round(subset(dataset_b, category=="value")$fraction, 2)*100, "%", sep=""), size=9)
geom_text(x=-1.5, y=2, label=paste("KPI b"), size=6, color="#a4a4a4")
pie_c <- ggplot(dataset_c, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category))
geom_rect()
scale_fill_manual(values=c("#2bbd97", "#64f0cb"))
coord_polar(theta="y")
xlim(c(-1, 4))
theme_void()
theme(legend.position = "none")
geom_text(x=0.5, y=2, label=paste(round(subset(dataset_c, category=="value")$fraction, 2)*100, "%", sep=""), size=9)
geom_text(x=-1.5, y=2, label=paste("KPI c"), size=6, color="#a4a4a4")
##### Display plots
ggarrange(pie_a,
pie_b,
pie_c,
nrow=1, ncol=3)