Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#141 enforce factors for legend and di…
Browse files Browse the repository at this point in the history
…screte scales of dataMapping
  • Loading branch information
pchelle committed Apr 23, 2021
1 parent 33ab5b9 commit 860a6a8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
10 changes: 8 additions & 2 deletions R/boxwhisker-datamapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ BoxWhiskerDataMapping <- R6::R6Class(
if (isOfLength(self$x, 0)) {
data$legendLabels <- factor("")
}

# Transform names into functions for aggregation summary
boxWhiskerLimitsFunctions <- sapply(self$boxWhiskerLimits, match.fun)

Expand All @@ -75,6 +75,9 @@ BoxWhiskerDataMapping <- R6::R6Class(

# Dummy variable for aesthetics
boxWhiskerLimits$legendLabels <- factor("")
if(!isOfLength(self$x, 0)){
boxWhiskerLimits[,self$x] <- as.factor(boxWhiskerLimits[,self$x])
}

return(boxWhiskerLimits)
},
Expand All @@ -89,7 +92,7 @@ BoxWhiskerDataMapping <- R6::R6Class(
if (isOfLength(self$x, 0)) {
data$legendLabels <- factor("")
}

# Transform names into functions for aggregation summary
outlierLimitsFunctions <- sapply(self$outlierLimits, match.fun)

Expand Down Expand Up @@ -120,6 +123,9 @@ BoxWhiskerDataMapping <- R6::R6Class(

# Dummy variable for aesthetics
outliers$legendLabels <- factor("")
if(!isOfLength(self$x, 0)){
outliers[,self$x] <- as.factor(outliers[,self$x])
}

return(outliers)
}
Expand Down
25 changes: 15 additions & 10 deletions R/datamapping-xygroup.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ XYGDataMapping <- R6::R6Class(

# All possible Groupings are listed in the enum LegendTypes
for (groupType in LegendTypes) {
if (!is.null(self$groupMapping[[groupType]]$group)) {
if (isOfLength(self$groupMapping[[groupType]]$group, 0)) {next}
grouping <- self$groupMapping[[groupType]]

groupVariables <- grouping$group
Expand All @@ -76,15 +76,20 @@ XYGDataMapping <- R6::R6Class(
groupVariables <- utils::head(groupVariables, -1)
}
validateMapping(groupVariables, data)
self$data[, grouping$label] <- grouping$getCaptions(data, metaData)
# Dummy variable for default aesthetics
# Will be used to define legend labels
self$data$legendLabels <- ifnotnull(
self$data$legendLabels,
paste(self$data$legendLabels, grouping$getCaptions(data, metaData), sep = "-"),
grouping$getCaptions(data, metaData)
)
}
# Enforce grouping variables to be factors
self$data[, grouping$label] <- as.factor(grouping$getCaptions(data, metaData))

# Dummy variable for default aesthetics that will be used to define legend labels
legendLabels <- self$data$legendLabels %||% grouping$getCaptions(data, metaData)

# Prevent duplication of legend if groupings are the same
if(all(as.character(legendLabels) == as.character(grouping$getCaptions(data, metaData)))){
self$data$legendLabels <- legendLabels
next
}
self$data$legendLabels <- as.factor(paste(as.character(self$data$legendLabels),
as.character(grouping$getCaptions(data, metaData)),
sep = "-"))
}

if (is.null(self$data$legendLabels)) {
Expand Down
14 changes: 14 additions & 0 deletions R/tornado-datamapping.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ TornadoDataMapping <- R6::R6Class(

self$lines <- lines
self$sorted <- sorted %||% TRUE
},

#' @description Check that \code{data} variables include map variables
#' @param data data.frame to check
#' @param metaData list containing information on \code{data}
#' @return A data.frame with map and \code{defaultAes} variables.
#' Dummy variable \code{defaultAes} is necessary to allow further modification of plots.
checkMapData = function(data, metaData = NULL) {
mapData <- super$checkMapData(data, metaData)
# Enforce y to be a discrete variable preventing crashes from numerical values
if(!isOfLength(self$y, 0)){
mapData[,self$y] <- as.factor(mapData[,self$y])
}
return(mapData)
}
)
)
25 changes: 24 additions & 1 deletion man/TornadoDataMapping.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 860a6a8

Please sign in to comment.