below is the reproducible example , for example I am applying below code
sourcing functions file in rmd and applying this function file in chunks in rmd as shown in above picture.
df<-data.frame(
aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,2,4,4,4,1,2,5,3,5),
col2=c(250,1100,100,750,400,100,200,700,500,700,200,600,200,200,600,300,400,300,200,500,700,500,600,400,400,600,500,600,400,100,700,300,200,700,700,200,300,700,200,400)
)
df$col1 <-factor(df$col1, levels = c(1,2,3,4,5), labels = c("CA","LA","MA","NY","MT"))
library(data.table)
library(dplyr)
library(ggplot2)
library(flextable)
library(scales)
library(stringr)
tab_std<-function(data, var, Name_of_variable, footer) {
data <- data[!is.na(data[[var]]), ]
T1 <- as.data.frame(table(data[[var]]))
all <- sum(T1[, 2])
T1 <- T1 %>% mutate(
!!Name_of_variable := as.character(Var1),
"Percent" = round(Freq * 100 / all),
"N" = as.numeric(Freq)
) %>%
select(!!Name_of_variable, "Percent", "N")
T1 <- flextable::flextable(T1)
T1
}
Teal<-c("#005E5D","#00968F","#98DBCE","#004C6C","#0077A0","#9CD9E4")
freq_for_charts<- function(data,var){
data<- data[!is.na(data[[var]]),]
T1<-as.data.frame(table(data[[var]]))
T1<-T1%>% mutate(Q=as.character(Var1),Freq=as.numeric(Freq))%>% select(Q,Freq)
all<-sum(T1[,2])
T1[, 2:ncol(T1)]<- sapply(T1[, 2:ncol(T1)],function(x) ifelse(all<3,NA,round(x/all,4)))
T1}
donut <- function(data, var) {
table <- freq_for_charts(data, var) %>% filter(Freq != 0) %>% mutate(Freq = Freq * 100)
ggplot(table, aes(x = 2, y = Freq, fill = Q)) +
geom_bar(stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0) +
geom_text(aes(label = paste(round(Freq), "%")), color = "white", fontface = "bold", size = 4,
position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = Teal) +
theme_void() + theme(legend.text = element_text(size = 12)) +
xlim(0.5, 2.5) + theme(legend.position = "bottom", legend.title = element_blank()) +
guides(fill = guide_legend(nrow = 2, byrow = TRUE))
}
single_bar <- function(data, var, SORT) {
table <- freq_for_charts(data, var) %>% filter(Freq != 0) %>% mutate(Freq = Freq * 100)
table$Q <- factor(table$Q, levels = rev(levels(data[[var]])))
switch (SORT,
"TRUE" = g <- ggplot(table, aes(x=reorder(Q,Freq), y=Freq)),
"FALSE"= g <- ggplot(table, aes(x=Q, y=Freq)))
chart <- g +
geom_bar(stat = "identity", width = 0.5, fill = "#005E5D") +
labs(x = NULL, y = NULL, subtitle = NULL) +
ylab(NULL) +
coord_flip() +
geom_text(aes(label = paste0(round(Freq), "%")),
fontface = "bold", color = "#404040", size = 3, hjust = -0.5,
position = position_stack()) + # this line centralizes text
scale_y_continuous(expand = c(0, 0), labels = number_format(suffix = "%"),
limits = c(0,max(table$Freq,na.rm = TRUE) + max(table$Freq,na.rm = TRUE) / 5)) +
scale_x_discrete(labels = function(x) str_wrap(x, width = 25))
return(chart)
}
tab_std(data=df, var = "col1", Name_of_variable = "state")
donut(df,"col1")
tab_std(data=df, var = "col1", Name_of_variable = "state")
single_bar(df,"col1", SORT = TRUE)