density.gg <- function (measure = 'p_', df_train = ds1, df_test = ds2, df_live = dsl,
xlim = NULL, ylim = NULL,
DT = F) {
library(tidyverse) %>% invisible()
# Train
if (!is.null(df_train)) {
df_train <- as.data.frame(df_train)
train <- df_train[, measure] %>% as.tibble() %>% mutate(Data = 'Train')
}
else if (is.null(df_train)) {
train <- NULL
}
# Test
if (!is.null(df_test)) {
df_test <- as.data.frame(df_test)
test <- df_test[, measure] %>% as.tibble() %>% mutate(Data = 'Test')
}
else if (is.null(df_test)) {
test <- NULL
}
# Live
if (!is.null(df_live)) {
df_live <- as.data.frame(df_live)
live <- df_live[, measure] %>% as.tibble() %>% mutate(Data = 'Live')
}
else if (is.null(df_live)) {
live <- NULL
}
all <- bind_rows(train, test, live)
colnames(all)[1] <- 'x'
density_plot <- all %>% mutate(Data = factor(Data, levels = c('Train', 'Test', 'Live'))) %>%
ggplot(aes(x = x, group = Data, colour = Data)) +
geom_density(lwd = 0.75) +
ggtitle('Density Plot') + xlab(measure) + ylab('Density') +
scale_color_manual(values = MixPalette) +
theme(
panel.background = element_rect(fill = "gray93",
colour = "gray93",
size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white"),
plot.background = element_rect(fill = "grey99"),
plot.title = element_text(size = 16, face = "bold"),
axis.title.x = element_text(size = 12, face="bold"),
axis.title.y = element_text(size = 12, face="bold"),
legend.text = element_text(color = "black",
size = 11,
face = "bold"),
legend.background = element_rect(fill = "gray93",
size = 0.5,
linetype = "solid",
color = "gray93"),
legend.title = element_blank(), legend.direction = "vertical",
text = element_text(family = 'Calibri')
)
if (!is.null(xlim)) {
density_plot <- density_plot + xlim(xlim)
}
if (!is.null(ylim)) {
density_plot <- density_plot + ylim(ylim)
}
print(density_plot)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.