Skip to content

Commit

Permalink
Merge pull request #110 from Roche/na-handling
Browse files Browse the repository at this point in the history
Handling NA values in factor columns
  • Loading branch information
cosi1 authored May 13, 2022
2 parents cd80f0d + 4325724 commit 5e4ff02
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.9
### Bugs
* fix handling NA values in factor columns

## 0.8.8
### Bugs
* fix a bug that occurs when there are NAs in x/y mappings (submitted by @zzawadz as #108)
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ggtips
Type: Package
Title: Interactive Tooltips for ggplots
Version: 0.8.8
Version: 0.8.9
Authors@R: c(person("Jakub", "Jankiewicz", role = "aut",
email = "[email protected]"),
person("Michal", "Jakubczak", role = "ctb",
Expand Down
3 changes: 2 additions & 1 deletion R/contents.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ unmapFactors <- function(df, origin, plot, layerData) {
# Map values in the column to the original values
column <- df[[name]]
asFactor <- factor(column, levels = unique(column))
df[[name]] <- levels(origColumn)[asFactor]
lvls <- as.character(unique(origColumn))
df[[name]] <- lvls[asFactor]
} else {
if (length(origColumn) == nrow(df)) {
# Simply add the column from the original data frame
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test_contents.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,29 @@ test_that("Warning message will be displayed when key specified in varDict
regexp = "The following variables are set as keys in varDict but are missing in plot data: notExistingKey1, notExistingKey2"
)
})

test_that("NA values in factors are handled correctly", {
# prepare an iris dataset with species releveled and NA introduced
irisNA <- iris
irisNA$Species[irisNA$Species == "versicolor"] <- NA
irisNA$Species <- relevel(irisNA$Species, "virginica")
varDict <- list(Species = "Species", Sepal.Length = "Sepal Length")
# create a ggplot and build it
testPlot <- ggplot(irisNA, mapping = aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point(mapping = aes(colour = Species))
testGrob <- ggplot_build(testPlot)
# get tooltip data for the plot
fullTooltipData <- ggtips:::getTooltipData(
plot = testPlot,
built = testGrob,
varDict = varDict,
plotScales = NULL,
callback = NULL
)[[1]]
fullTooltipData$Species <- as.character(fullTooltipData$Species)
expectedTooltips <- irisNA[c("Species", "Sepal.Length")]
expectedTooltips$Species <- as.character(expectedTooltips$Species)
expectedTooltips$Sepal.Length <- format(expectedTooltips$Sepal.Length)
names(expectedTooltips) <- c("Species", "Sepal Length")
expect_equal(fullTooltipData, expectedTooltips)
})

0 comments on commit 5e4ff02

Please sign in to comment.