0

I have a plot where I need to delete the decimals from the percentage numbers. The data is already rounded (two decimals), an example of the data:

> head(df$X)
[1] 0.05 0.28 0.08 0.19 0.33 

And then I plot it, using scale_x_continuous() to transform it to percentage format, but as you can see in the image below they still have one decimal and I need those numbers without decimals.

Output

This is the code for the graph:

ggplot(df, aes(x=df$X, y=DF$Y)) + 
    geom_point(
        colour="red",
        size=5
        ) + 
  geom_text(label=df$Label, vjust=-2, family="sans", fontface="bold") +
  labs(x="X", y="Y") + xlim(-1,1) + ylim(-1,1) + scale_x_continuous(labels=scales::percent) + scale_y_continuous(labels=scales::percent)

The closest answer I have found is this one, but I don't need to paste the "%" symbol as I already have it.

Chris
  • 2,019
  • 5
  • 22
  • 67
  • 1
    Side note: Don't use `data$col` inside `aes()`. It will still work for simple plots, but it will cause problems with many of ggplot's advanced features. Just do `ggplot(df, aes(x = X, y = Y))`. And you should probably put the `label` inside `aes` too: `geom_text(aes(label = Label), ...)` – Gregor Thomas Jan 16 '19 at 18:46
  • 1
    @Gregor `scales::percent` doesn't take a digits argument, but it *does* take an accuracy argument. It's formatted differently, though; worth looking at the examples in the docs – camille Jan 16 '19 at 18:57

1 Answers1

6

scales::percent has an accuracy argument that you can modify. This should give you what you want:

ggplot(df, aes(x, y)) + 
  geom_point(
    colour="red",
    size=5
  ) + 
  labs(x="X", y="Y") + 
  xlim(-1,1) + 
  ylim(-1,1) + 
  scale_x_continuous(labels = function(x) scales::percent(x, accuracy = 1)) + 
  scale_y_continuous(labels = function(x) scales::percent(x, accuracy = 1))
dave-edison
  • 3,666
  • 7
  • 19