Skip to content

Do not remove levels only containing NA data if na.rm is set to FALSE #6018

Closed
@MoREpro

Description

@MoREpro

The option na.rm in layers, e.g. geom_boxplot or geom_point is not working as one would expect. Even if the option na.rm is set to FALSE, the removal of NA values occur and factors containing only NA values are dropped in the plot.
I propose to improve this option as follows:

  • na.rm = NA (default): remove NAs with a warning (as now with na.rm=FALSE)
  • na.rm = FALSE: do not remove NA values (respective factor levels only containing NA values)
  • na.rm = TRUE: remove NAs without a warning (as now)

There are occasions where the current behaviour of removing NA values and associated factor levels containing only NA values is disadvantageous, e.g. if layers added later to the plot contain data for these factor levels. Then the current behaviour leads to misaligned graphical representation.
image

Code snippet demonstrating, how this plot was created:

p1 <- ggplot(data = plot_data_complete, aes(y = value_ros, x = sampling_site, fill = analysed_matrix)) +
    geom_boxplot(notch = TRUE) +
    scale_y_log10(breaks = log_breaks, minor_breaks = log_minor_breaks) +
    theme_bw()+
    facet_wrap(~ substance, ncol = 2, scales = "free_y")+
    geom_text(data = count_data, aes(y = max_y * 0.9, label=count),
              position = position_dodge(width = 0.75),
              size = 3, angle = 70, show.legend = FALSE)
# Adding an element using the x-positions from first ggplot-object:
p1 + geom_segment(data = ana_threshold,
                  aes(y = min_lod, yend = max_lod, x = ggplot_build(p1)$data[[2]]$xmin,# - 0.05,
                      xend = ggplot_build(p1)$data[[2]]$xmax, col = "LOD"),# + 0.05),
                  inherit.aes = FALSE, show.legend =TRUE) +
    scale_colour_manual("LOD shown where\nrelevant:", values = c("LOD" = "red"))+
    xlab("sampling site") +
    ylab("concentration in µg/l") +
    labs(fill = "analysed matrix:")

If it is necessary to provide a minimal example I can do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions