0

I'm trying to remove zero values from my visual using the plot_xtab function from the sjPlot package. I tried using drop.empty = FALSE, but that didn't seem to help.

I just don't like the way the "n=0 (0%)" looks. Does anyone know if this function still works or if there is an alternative?

Julien
  • 1,613
  • 1
  • 10
  • 26
  • 3
    Need data and code. Don’t forget the `library` calls. – IRTFM Jul 31 '22 at 04:16
  • Welcome to SO, Sydney Ito! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 31 '22 at 08:23

1 Answers1

0

You could remove the rows in the data from your plot with zeros. So if you save the plot as an object, you can extract the data. By conditionally removing the rows with zeros, these are gone in the plot. I don't have your data, so I created a reproducible example:

df <- data.frame(x = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4),
                 y = c(1,0,1,2,3,2,1,0,4,3,1,2,0,2,1, 0))
library(sjPlot)
p <- plot_xtab(df$x, df$y)
p

# remove rows with prc = 0:
p$data <- p$data[p$data$prc != 0, ]
p

Created on 2022-08-01 by the reprex package (v2.0.1)

As you can see, the "n=0 (0%)" are gone!

Quinten
  • 35,235
  • 5
  • 20
  • 53