I use this code:
library(sjPlot)
mtcars |>
plot_frq(cyl)
and I get
I would like to remove the parentheses and round the percentage numbers to the nearest whole number. So that I get 34% instead of (34.4%). I am looking for a direct way to access the ggplot elements.
I tried accessing the underlying ggplot object but was unsuccessful.
Thank you very much!
I use this code:
library(sjPlot)
mtcars |>
plot_frq(cyl)
and I get
I would like to remove the parentheses and round the percentage numbers to the nearest whole number. So that I get 34% instead of (34.4%). I am looking for a direct way to access the ggplot elements.
I tried accessing the underlying ggplot object but was unsuccessful.
Thank you very much!
Share Improve this question asked Nov 20, 2024 at 9:49 TarJaeTarJae 79.3k6 gold badges28 silver badges88 bronze badges Recognized by R Language Collective 1 |1 Answer
Reset to default 3I agree with @qdread that it might be easier to build the plot from scratch. But if you want to do the hacky approach then one option would be to get rid of the labels added by plot_frq
and add your own:
library(sjPlot)
library(ggplot2)
p <- mtcars |>
plot_frq(cyl)
p$layers[[2]]$aes_params$label <- NULL
p +
aes(label = paste(
.data$frq,
scales::percent(.data$frq / sum(.data$frq), accuracy = 1),
sep = "\n"
))
plot_frq
the number of decimal places and the parentheses are hard-coded in: github/strengejacke/sjPlot/blob/… . Seems like it would be easier to just write your own code to make the plot you want rather than trying to modify theplot_frq
output – qdread Commented Nov 20, 2024 at 10:21