ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

"scale_y_log" with "jitter_dodge" misbehavior (using color to create 3 plots at one x value)

Open mfoos opened this issue 4 years ago • 3 comments

Hello! I have this plot in a Shiny app and so there are a lot of possibly combinations and one of my users pointed this out to me today. In the Real World this x axis is timepoints, and I've just pulled one out for the reprex.

Basically, for a given x value, I want 3 boxplots so I used aes(color) to make them. When one of my factors only has zero as a value, and I apply a log10 transform (thus creating a -Inf) it throws off the x position of the dodge. This does not happen when x and color are the same value.

library(reprex)
library(ggplot2) # ggplot2_3.3.2

df <- data.frame(cat_id = as.character(1:22),
                 species = "cat",
                 color = c("tabby", rep("tortie", 20), rep("black", 1)),
                 floofiness = c(runif(21, 1, 10), 0),
                 nose_warmth = c(runif(22, 1, 10)))

Expected behavior

# y = floofiness (only black value = 0)
ggplot(df, aes(x = species, y = floofiness, color = color)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitterdodge(jitter.width = .1)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))


# y = nose_warmth (no zeroes) + log
ggplot(df, aes(x = species, y = nose_warmth, color = color)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitterdodge(jitter.width = .1)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_log10() 

Not expected behavior

# y = floofiness (only black value = 0) + log
ggplot(df, aes(x = species, y = floofiness, color = color)) +
  geom_boxplot(outlier.shape = NA) +
  geom_point(position = position_jitterdodge(jitter.width = .1)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_log10() # this is the only difference
#> Warning: Transformation introduced infinite values in continuous y-axis

#> Warning: Transformation introduced infinite values in continuous y-axis
#> Warning: Removed 1 rows containing non-finite values (stat_boxplot).

Created on 2020-10-30 by the reprex package (v0.3.0)

mfoos avatar Oct 30 '20 19:10 mfoos