2

A really simple question (I am fairly new to R):

I have created a ggbarplot using ggpubr package, and whilst I can get the y scale to be formatted as a percentage (e.g. 80% instead of 0.8), I am unable to change the format of the data labels.

Here is the code:

ggbarplot(x = "Trophic level",
            y = "percent.present",
            fill = "Structure type",
            color = "Structure type",
            position = position_dodge(), 
            ylab = "% of species that used underpasses",
            ggtheme = theme_bw(), 
            title = "Proportion of species in area that used underpasses",
            palette = "Set2",
            label = TRUE, lab.nb.digits = 2) %>%
  ggpar(legend = "bottom") +
  yscale("percent", .format = TRUE)

And here is the resulting plot - note the labels are showing up as 0.8 instead of 80%.

Plot1

How would I amend the code to format the data labels to a percentage too?

dput output here:

structure(list(`Trophic level` = c("Apex predator", "Apex predator", 
"Megaherbivore", "Megaherbivore"), `Structure type` = c("All underpasses", 
"Non-viaduct culverts", "All underpasses", "Non-viaduct culverts"
), percent.present = c(1, 1, 0.8, 0.2)), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))
hannahdv35
  • 23
  • 3
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data to run your code and figure out a solution for your issue. – stefan Jan 24 '23 at 09:39
  • 1
    After a look at the docs I think that there isn't an option to format the labels as percentages. But one option would be to add the text labels via a `geom_text` – stefan Jan 24 '23 at 09:42
  • it might be easier to multiply the data column used as the y-axis by 100, then use a regular y-axis scale instead of % – George Savva Jan 24 '23 at 09:47
  • Could you please share some reproducible data using `dput`? – Quinten Jan 24 '23 at 17:38

1 Answers1

0

One option would be to pass a vector of labels to the label argument of ggbarplot, where I use scales::percent to format as percentages.

library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)

ggbarplot(dat,
          x = "Trophic level",
          y = "percent.present",
          fill = "Structure type",
          color = "Structure type",
          position = position_dodge(),
          ylab = "% of species that used underpasses",
          ggtheme = theme_bw(),
          title = "Proportion of species in area that used underpasses",
          palette = "Set2",
          label = scales::percent(dat$percent.present, accuracy = .01)
) %>%
  ggpar(legend = "bottom") +
  yscale("percent", .format = TRUE)

And as I mentioned in my comment, another option would be to add the labels via a geom_text layer:

ggbarplot(dat,
  x = "Trophic level",
  y = "percent.present",
  fill = "Structure type",
  color = "Structure type",
  position = position_dodge(),
  ylab = "% of species that used underpasses",
  ggtheme = theme_bw(),
  title = "Proportion of species in area that used underpasses",
  palette = "Set2"
) %>%
  ggpar(legend = "bottom") +
  yscale("percent", .format = TRUE) +
  geom_text(
    aes(
      group = `Structure type`,
      label = scales::percent(percent.present, accuracy = .01)
    ),
    position = position_dodge(.9),
    vjust = -0.4,
  )

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    thank you so much, the first option is exactly what I was trying to do but couldn't figure out where to use the scales::percent() function. :) – hannahdv35 Jan 25 '23 at 10:37