Skip to content

Commit 7363050

Browse files
author
hbaniecki
committed
change plot_res_dens subtitles
1 parent 7bc0f4b commit 7363050

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

R/plot_residual_density.R

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,23 @@ plot_residual_density <- function(object, ..., variable = "") {
5858
model_count <- nlevels(df$`_label_`)
5959
df$`_ord_` <- paste(rev(as.numeric(df$`_label_`)), df$`_label_`)
6060

61-
if (!is.null(variable)) {
62-
split <- FALSE
63-
if (variable != "") split <- TRUE
64-
}
61+
split <- TRUE
6562

66-
# set value for label of the X axis
67-
if (is.null(variable)) {
68-
lab <- "observations"
69-
split <- TRUE
70-
} else if (variable == "_y_") {
71-
lab <- "target variable"
72-
} else if (variable == "_y_hat_") {
73-
lab <- "actual response"
74-
} else {
75-
lab <- as.character(df$`_variable_`[1])
63+
if (!is.null(variable)) {
64+
if (variable == "") split <- FALSE
7665
}
7766

78-
levels(df$`_div_`) <- gsub("\\s.+\\s|\\s\\s", paste0(" ", lab, " "), levels(df$`_div_`))
67+
var_split <- "`_label_`"
7968

8069
# arguments differ depending on splitting or not
8170
if (split == TRUE) {
82-
var_split <- "`_label_`"
8371
colours <- theme_drwhy_colors(nlevels(df$`_div_`))
8472
legend_pos <- "bottom"
85-
# if (model_count == 1) legend_pos <- "none"
8673
legend_just <- "center"
87-
split_by <- unique(df$`_label_`)
88-
} else if (split == FALSE) {
89-
var_split <- "`_label_`"
74+
} else {
9075
colours <- theme_drwhy_colors(model_count)
9176
legend_pos <- "top"
9277
legend_just <- c(1, 0)
93-
# if (model_count == 1) legend_pos <- "none"
94-
split_by <- unique(df$`_label_`)
9578
}
9679

9780
if (model_count == 1) legend_pos <- "none"
@@ -102,7 +85,7 @@ plot_residual_density <- function(object, ..., variable = "") {
10285
geom_vline(xintercept = 0, colour = "darkgrey") +
10386
annotate("segment", x = -Inf, xend = Inf, y = -Inf, yend = -Inf, colour = "#371ea3") +
10487
scale_color_manual(values = colours) +
105-
scale_fill_manual(values = colours, breaks = split_by) +
88+
scale_fill_manual(values = colours, breaks = unique(df$`_label_`)) +
10689
guides(fill = guide_legend(override.aes = list(alpha = 0.5))) +
10790
theme_drwhy() +
10891
theme(axis.line.x = element_line(color = "#371ea3"),

R/plot_support_function.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,18 @@ obs_influence_add <- function(object, nlabel) {
184184
get_division <- function(modelData, variable) {
185185
df <- modelData
186186

187-
if (!is.null(variable)) {
188-
if (variable == "") variable <- NULL
189-
}
190-
191187
if (is.null(variable)) {
188+
variable <- "observation index"
189+
modelData$`_val_`<- 1:nrow(modelData)
190+
} else if (variable == "") {
191+
variable <- "observation index"
192192
modelData$`_val_`<- 1:nrow(modelData)
193+
} else if (variable == "_y_") {
194+
modelData$`_val_`<- modelData[, variable]
195+
variable <- "target variable"
196+
} else if (variable == "_y_hat_") {
197+
modelData$`_val_`<- modelData[, variable]
198+
variable <- "actual response"
193199
} else {
194200
modelData$`_val_`<- modelData[, variable]
195201
}
@@ -200,7 +206,9 @@ get_division <- function(modelData, variable) {
200206
} else {
201207
df$`_div_` <- unlist(modelData$`_val_`, use.names = FALSE)
202208
}
209+
203210
df$`_div_` <- factor(df$`_div_`)
211+
204212
rownames(df) <- NULL
205213
return(df)
206214
}

0 commit comments

Comments
 (0)