knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Specific test functions

Load the koboLoops Package

library("visualisationIMPACT")
library(magrittr)
library("ggthemes")
library(extrafont)
library(ggplot2)
library(gridExtra)
library(grid)

Example data frame

Here we are downloading from a file in test repository.

data<-read.csv("../tests/testthat/testdata.csv")

Barchart with average (numerical-categorical)

Calculate average, min and max

dependent.var = "hhnumberindividuals"
independent.var = "region" 

result <- data %>% dplyr::select(c( independent.var , dependent.var)) %>% dplyr::group_by_(independent.var) %>% dplyr::summarise_all(c(average=mean,min=min,max=max))

Barchart average with several categories

extrafont::loadfonts(device="win")  
sens <- orientation_plot(.data = result, x.label = region ,max_nbr_var = 6,size_max_label = 10)

result$min <- c(2,2,5,3,NA,3,3)
theplot <- barchart_impact(.data = result, x = region, y = average, infimum_error = min, supremum_error = max, sens.barchart = sens, percent = TRUE, scale.percent = 1)

save_graph(ggplot_object = theplot, filename = "test1.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "test1_report.jpg", path = "./", type.output = "report" )

Barchart with percentages

result$average <- c(0.5,0.50, 0.30, 0.70, 0.3, 0.8, 0.1)
sens <- orientation_plot(.data = result, x.label = region ,max_nbr_var = 6,size_max_label = 10)

theplot <- barchart_impact(.data = result, x = region, y = average, sens.barchart = sens, percent = TRUE, scale.percent = 100)

save_graph(ggplot_object = theplot, filename = "test2.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "test2_report.jpg", path = "./", type.output = "report" )

Barchart with percent >100%

result$average <- c(200, 50,40,30, 10,5,40)

testthat::expect_error(barchart_impact(.data = result, x = region, y = average, infimum_error = min, supremum_error = max, sens.barchart = sens, percent = TRUE, scale.percent = 100))

Grouped barchart

regions.value=c("capitalcentral","capitalcentral", "north","north","south","south","west","west")
choices_value=c("yes","no","yes","no","yes","no","yes","no")
result_percent= c(0.5,0.50, 0.30, 0.7012, 0.45,0.55, 0.90,0.10)
min_value <- result_percent-5
max_value <- result_percent+5



table_result <- data.frame(region=regions.value, choice=choices_value, percents=result_percent, infimum_error=min_value, supremum_error=max_value)
sens <- orientation_plot(table_result, region, 6, 12)
theplotFS <- grouped_barchart_impact(table_result, region , choice , percents, infimum_error, supremum_error, sens.barchart=sens, percent = TRUE, scale.percent = 100 )

sens <- orientation_plot(table_result, region, 10, 20)
theplotreport <- grouped_barchart_impact(table_result, region , choice , percents, infimum_error, supremum_error, sens.barchart=sens, percent = TRUE, scale.percent = 100 )

save_graph(ggplot_object = theplotFS, filename = "test3.jpg", path = "./" )
save_graph(ggplot_object = theplotreport, filename = "test3_report.jpg", path = "./", type.output = "report" )
regions.value=c("capitalcentral","capitalcentral","capitalcentral","north", "north","north","south","south","south","west","west","west")
choices_value=c("yes","no", "bb","yes","no", "bb","yes","no","bb","yes","no","bb")
 result_percent=c(40,50,100,30,200,30,50,70, 20,50, 60, 70 )
min_value <- result_percent - 10
max_value <-result_percent + 20

table_result <- data.frame(region=regions.value, choice=choices_value, percents=result_percent, infimum_error=min_value, supremum_error=max_value)

sens <- orientation_plot(table_result, region, 6, 12)
theplotFS <- grouped_barchart_impact(table_result, region , choice , percents, infimum_error, supremum_error, sens.barchart=sens, percent = FALSE ) 

sens <- orientation_plot(table_result, region, 10, 20)
theplotreport <- grouped_barchart_impact(table_result, region , choice , percents, infimum_error, supremum_error, sens.barchart=sens, percent = FALSE ) 

save_graph(ggplot_object = theplotFS, filename = "test4.jpg", path = "./" )
save_graph(ggplot_object = theplotreport, filename = "test4_report.jpg", path = "./", type.output = "report" )
regions.value=c("capitalcentral","capitalcentral","capitalcentral")
choices_value=c("yes","no", "bb")
result_percent=c(40,50,100)
min_value <- result_percent - 10
max_value <-result_percent + 20
sens <- orientation_plot(.data = result, x.label = region ,max_nbr_var = 6,size_max_label = 10)

table_result <- data.frame(region=regions.value, choice=choices_value, percents=result_percent, infimum_error=min_value, supremum_error=max_value)

theplot <- grouped_barchart_impact(table_result, region , choice , percents, infimum_error, supremum_error, sens.barchart=sens, percent = FALSE ) 

save_graph(ggplot_object = theplot, filename = "test5.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "test5_report.jpg", path = "./", type.output = "report" )

One boxplot

regions.value="capitalcentral"
median_value=20
first_quantile <- 5
third_quantile <- 30
min_value <- 2.5
max_value <- 80
outliers_min <- 1
outliers_max <- NA


result_box <- data.frame(region=regions.value, median=median_value, min= min_value, max = max_value, first_quantile = first_quantile, third_quantile = third_quantile,outlier_min = outliers_min, outlier_max=outliers_max )

theplot <- boxplot_impact(result_box, region, "cashincome", median, first_quantile, third_quantile, min, max,outlier_min, outlier_max, sens.boxplot = "vertical")

save_graph(ggplot_object = theplot, filename = "boxplot1.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "boxplot1_report.jpg", path = "./", type.output = "report" )

One plot with several boxplot

regions.value=c("capitalcentral", "north","south","west")
median_value=c(20,50,100,30)
first_quantile <- c(5,30,90, 10)
third_quantile <- c(30,70, 130,50)
min_value <- c(5,30,90, 10) - 1.5
max_value <- c(30,70, 130,50) + 50
outliers_min <- c(1,NA,70, 5)
outliers_max <- c(NA,150, 200,120)

result_box <- data.frame(region=regions.value, median=median_value, min= min_value, max = max_value, first_quantile = first_quantile, third_quantile = third_quantile,outlier_min = outliers_min, outlier_max=outliers_max )

theplot <- boxplot_impact(result_box, region, "cashincome", median, first_quantile, third_quantile, min, max,outlier_min, outlier_max, sens.boxplot = "vertical")
save_graph(ggplot_object = theplot, filename = "boxplot2.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "boxplot2_report.jpg", path = "./", type.output = "report" )

Grouped boxplot

For 2 sub variables

regions.value=c("capitalcentral","capitalcentral", "north","north","south","south","west","west")
choices_value=c("yes","no","yes","no","yes","no","yes","no")

median_value=c(40,50,100,30,200,300,50,70 )
first_quantile <- c(20,30,90, 10,100,200,20,50)
third_quantile <- c(60,70, 130,50,250, 450, 60, 80)
min_value <- c(20,30,90, 10,100,200,20,50) - 10
max_value <- c(60,70, 130,50,250, 450, 60, 80) + 50
outliers_min <- c(1,10,NA, NA, NA, 50, NA, NA)
outliers_max <- c(NA,150, 200,120, NA, NA, NA, 200)


table_result <- data.frame(region=regions.value, subsection=choices_value,  median = median_value, min = min_value, max = max_value, first_quantile, third_quantile, outliers_min, outliers_max)

theplot <- grouped_boxplot_impact(table_result, region, subsection, "cashincome", median, min, max, first_quantile, third_quantile, outliers_min , outliers_max ,sens.boxplot = "horizontal") 
save_graph(ggplot_object = theplot, filename = "boxplot3.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "boxplot3_report.jpg", path = "./", type.output = "report" )

For 3 subvariables

regions.value=c("capitalcentral","capitalcentral","capitalcentral","north", "north","north","south","south","south","west","west","west")
choices_value=c("yes","no", "bb","yes","no", "bb","yes","no","bb","yes","no","bb")
median_value=c(40,50,100,30,200,300,50,70, 20,50, 60, 70 )
first_quantile <- c(20,30,90, 10,100,200,20,50, 10,40,50,60)
third_quantile <- c(60,70, 130,50,250, 450, 60, 80, 40,60,80,100)
min_value <- c(20,30,90, 10,100,200,20,50, 10,40,50,60) - 10
max_value <- c(60,70, 130,50,250, 450, 60, 80, 40,60,80,100) + 50
outliers_min <- c(1,10,NA, NA, NA, 50, NA, NA, 5,30,10,10)
outliers_max <- c(NA,150, 200,120, NA, NA, NA, 300,100, 200, 300, 250)

table_result <- data.frame(region=regions.value, subsection=choices_value,  median = median_value, min = min_value, max = max_value, first_quantile, third_quantile, outliers_min, outliers_max)

theplot <- grouped_boxplot_impact(table_result, region, subsection, "cashincome", median, min, max, first_quantile, third_quantile, outliers_min , outliers_max ,sens.boxplot = "horizontal")

save_graph(ggplot_object = theplot, filename = "boxplot4.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "boxplot4_report.jpg", path = "./", type.output = "report" )

Grouped boxplot don't take in account the order of the categorical variable

# regions.value=c("capitalcentral", "north","south","west","capitalcentral","north","south","west")
# choices_value=c("yes","yes","yes","yes","no","no","no","no")

regions.value=c("capitalcentral", "north","west","west","capitalcentral","north","south","south")
choices_value=c("yes","yes","yes","no","no","no","no","yes")

median_value=c(40,50,100,30,200,300,50,70 )
first_quantile <- c(20,30,90, 10,100,200,20,50)
third_quantile <- c(60,70, 130,50,250, 450, 60, 80)
min_value <- c(20,30,90, 10,100,200,20,50) - 10
max_value <- c(60,70, 130,50,250, 450, 60, 80) + 50
outliers_min <- c(1,10,NA, NA, NA, 50, NA, NA)
outliers_max <- c(NA,150, 200,120, NA, NA, NA, 200)


table_result <- data.frame(region=regions.value, subsection=choices_value,  median = median_value, min = min_value, max = max_value, first_quantile, third_quantile, outliers_min, outliers_max)

theplot <- grouped_boxplot_impact(table_result, region, subsection, "cashincome", median, min, max, first_quantile, third_quantile, outliers_min , outliers_max ,sens.boxplot = "horizontal") 

save_graph(ggplot_object = theplot, filename = "boxplot5.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "boxplot5_report.jpg", path = "./", type.output = "report" )

Missing sub values

# regions.value=c("capitalcentral", "north","south","west","capitalcentral","north","south","west")
# choices_value=c("yes","yes","yes","yes","no","no","no","no")

regions.value=c("capitalcentral", "north","west","west","capitalcentral","north","south","south")
choices_value=c(NA,"yes","yes","no","no",NA,"no","yes")

median_value=c(NA,50,100,30,200,300,50,70 )
first_quantile <- c(NA,30,90, 10,100,200,20,50)
third_quantile <- c(NA,70, 130,50,250, 450, 60, 80)
min_value <- c(NA,30,90, 10,100,200,20,50) - 10
max_value <- c(NA,70, 130,50,250, 450, 60, 80) + 50
outliers_min <- c(NA,10,NA, NA, NA, 50, NA, NA)
outliers_max <- c(NA,150, 200,120, NA, NA, NA, 200)


table_result <- data.frame(region=regions.value, subsection=choices_value,  median = median_value, min = min_value, max = max_value, first_quantile, third_quantile, outliers_min, outliers_max)

theplot <-grouped_boxplot_impact(table_result, region, subsection, "cashincome", median, min, max, first_quantile, third_quantile, outliers_min , outliers_max ,sens.boxplot = "horizontal") 

save_graph(ggplot_object = theplot, filename = "boxplot6.jpg", path = "./" )
save_graph(ggplot_object = theplot, filename = "boxplot6_report.jpg", path = "./", type.output = "report" )

Load different results to test barchart functions

output <- readRDS("../tests/testthat/output_hypegrammaR.RDS")
for(i in 1:4){ 
  data <- output$results[[i]]$summary.statistic
  sens <- orientation_plot(.data = data, x.label = dependent.var.value,max_nbr_var = 6,size_max_label = 12)
  theplot<- grouped_barchart_impact(data,x = dependent.var.value, subset.x = independent.var.value , y = numbers, infimum_error = min , supremum_error = max, sens.barchart = sens , percent = TRUE, scale.percent = 100)
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_FS.jpg"), path = "./" , type.output = "FS")
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_report.jpg"), path = "./", type.output = "report" )

}

data <- output$results[[1]]$summary.statistic
data$dependent.var.value <- gsub("priorityproblems.", "", data$dependent.var.value)

sens <- orientation_plot(.data = data, x.label = dependent.var.value,max_nbr_var = 6,size_max_label = 12)
theplot<- grouped_barchart_impact(data,x = dependent.var.value, subset.x = independent.var.value , y = numbers, infimum_error = min , supremum_error = max, sens.barchart = sens , percent = TRUE, scale.percent = 100)
save_graph(ggplot_object = theplot, filename = paste0(as.character(1),"plot_FS_bis.jpg"), path = "./" , type.output = "FS")
save_graph(ggplot_object = theplot, filename = paste0(as.character(1),"plot_report_bis.jpg"), path = "./", type.output = "report" )
for(i in c(5:8,20:25)){
  data <- output$results[[i]]$summary.statistic
  sens <- orientation_plot(.data = data, x.label = dependent.var.value,max_nbr_var = 6,size_max_label = 12)
  theplot<- barchart_impact(data,x = independent.var.value , y = numbers, infimum_error = min , supremum_error = max, sens.barchart = sens , percent = FALSE, scale.percent = 100)
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_FS.jpg"), path = "./" , type.output = "FS")
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_report.jpg"), path = "./", type.output = "report" )
}

Only NA in mandatory variable

i=13
data <- output$results[[i]]$summary.statistic
sens <- orientation_plot(.data = data, x.label = dependent.var.value,max_nbr_var = 6,size_max_label = 12 )
testthat::expect_error(plot<- barchart_impact(data,x = dependent.var.value , y = numbers, infimum_error = min , supremum_error = max, sens.barchart = sens , percent = FALSE))
for(i in c(14:19,26,29)){
  data <- output$results[[i]]$summary.statistic
  sens <- orientation_plot(.data = data, x.label = dependent.var.value,max_nbr_var = 6,size_max_label = 12 )
  theplot<- barchart_impact(data,x = dependent.var.value , y = numbers, infimum_error = min , supremum_error = max, sens.barchart = sens , percent = FALSE)
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_FS.jpg"), path = "./" , type.output = "FS")
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_report.jpg"), path = "./", type.output = "report" )

}
for(i in c(9:11,27,28)){
  data <- output$results[[i]]$summary.statistic
  sens <- orientation_plot(.data = data, x.label = dependent.var.value,max_nbr_var = 6,size_max_label = 12 )
  theplot<- barchart_impact(data,x = dependent.var.value , y = numbers, infimum_error = min , supremum_error = max, sens.barchart = sens , percent = FALSE)
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_FS.jpg"), path = "./", type.output = "FS" )
  save_graph(ggplot_object = theplot, filename = paste0(as.character(i),"plot_report.jpg"), path = "./", type.output = "report" )


}

Regression

df <- mtcars[, c("mpg", "cyl", "wt", "qsec", "vs", "gear")]

regression_impact(mtcars, disp, mpg, wt)
grouped_regression_impact(mtcars, disp, subset.x = gear, mpg, wt)


sharonorengo/visualisationIMPACT documentation built on Aug. 12, 2019, 3:34 p.m.