最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to tweak geom_text element in sjPlot - Stack Overflow

programmeradmin0浏览0评论

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 Looking at the source code of 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 the plot_frq output – qdread Commented Nov 20, 2024 at 10:21
Add a comment  | 

1 Answer 1

Reset to default 3

I 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"
  ))

发布评论

评论列表(0)

  1. 暂无评论