How to Round Percentage to 2 Decimal Places in Ggplot2

Adding percentages up to two decimals on to of ggplot bar chart

Just use sprintf:

sprintf("%0.2f%%", df$Avg_Cost)
# [1] "5.30%" "3.72%" "2.91%" "2.64%" "1.17%" "1.10%"

plotB <- ggplot(df, aes(x = reorder(Seller, Avg_Cost), y = Avg_Cost)) +
geom_col( width = 0.7) +
coord_flip() +
geom_bar(stat="identity", fill="steelblue") +
theme( panel.background = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_blank()) +
geom_text(aes(label = sprintf("%0.2f%%", Avg_Cost)), size=5, hjust=-.2 ) +
### ^^^^ this is your change ^^^^
ylim(0,6)

Sample Image

Not able to add percentage labels with two decimals in ggplot2 geom_text

You can use accuracy with the scales::percent() function:

geom_text(aes(label = scales::percent(round(df$values, digits = 4), accuracy = 0.01)), position = position_stack(vjust = 0.5))

Percentages don't round up when using percent(accuracy = 3L) in ggplot

The data you provide does not reproduce your problem, because the sample is too small.
Use accuracy = 0.1, this should solve it. Also minor edit with empty axis labels axis.title = element_blank()...

lebanon %>%
filter(!is.na(challenge)) %>%
count(challenge) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(fct_reorder(challenge, prop), y = prop, fill = prop)) +
geom_bar(stat = "identity", show.legend = FALSE) +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.1)) +
geom_text(aes(label = scales::percent(prop, suffix = "", accuracy = 0.1)),
position = position_dodge(width = 0.9), vjust = 0.4, hjust = -0.1, size = 5) +
labs(title = "What is the most important challenge facing Lebanon today?") +
theme_minimal() +
theme(axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15),
axis.title = element_blank(),
legend.text = element_text(size = 15),
plot.title = element_text(size = 25),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank()) +
coord_flip()

How to prevent scales::percent from adding decimal

Perhaps not a direct answer to your question, but I have used scales::percent_format and its accuracy argument ("Number to round to") in similar settings.

mtcars %>% 
count(cyl) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(x = cyl, y = prop)) +
geom_point() +
scale_y_continuous(labels = scales::percent_format(accuracy = 5L))

Sample Image


I think the behaviour of percent was changed in scales 1.0.0. See NEWS and updates in code here.

In geom_text, can labels=scales::percent be rounded?

Here is a minimal change to your current code that will do what you want:

library(ggplot2)
library(scales)

d <- mtcars

g <- ggplot(d, aes(gear)) +
geom_bar(aes(y = (..count..)/sum(..count..), fill=factor(..x..)), stat= "count")+
geom_text(aes(label = scales::percent(round((..count..)/sum(..count..),2)),
y= ((..count..)/sum(..count..))), stat="count",
vjust = -.25)

I have added a call to round(...,2) in your division that will round the ratio before passing it to percent.


Personally, I would do this outside of ggplot for clarity of code.

library(ggplot2)
library(scales)
library(dplyr)

d <- mtcars %>%
group_by(gear) %>%
summarise(Count = n()) %>%
mutate( gear = factor(gear),
Ratio = Count / sum(Count),
label = percent(Ratio %>% round(2)))

g <- ggplot(d, aes(x=gear,y=Ratio,label=label,fill=gear)) +
geom_bar(stat='identity') +
geom_text(vjust=0)
g

When I have to go back and look at that in 6 months, it will be a lot easier to figure out what I did.

Deleting decimals in percentages axis - R

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

How to always have fixed number of decimals in ggplot - geom_text

This is built-in to scales::percent. There is an accuracy argument which is described as "the number to round to".

ggplot(mtcars, aes(factor(cyl))) + 
geom_bar(color = "steelblue", fill = "#00AFBB", na.rm = T) +
scale_fill_discrete(drop=FALSE) +
scale_x_discrete(drop=FALSE) +
geom_text(aes(label=scales::percent(..count../sum(..count..), accuracy = 0.01)),
stat='count',vjust = -0.5, size = 4)

how to put exact number of decimal places on label ggplot bar chart

You could try the following as it rounds to two digits and prints two digits after the decimal.

ggplot(srednie, aes(x=factor(strefa), y=a, label=sprintf("%0.2f", round(a, digits = 2)))) +
geom_bar(position=position_dodge(), stat="identity", colour="darkgrey", width = 0.5) +
theme(legend.position="none",axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank()) +
geom_text(size = 4, hjust = 1.2) +
coord_flip(ylim = c(1,6))+
xlab("") +
ylab("")

The only modification was changing your code from

round(a, digits = 2)

to

sprintf("%0.2f", round(a, digits = 2))

Sample Image

R, ggplot: Decimals on y-axis

With the new version of scales you can use:

scale_y_continuous(labels = scales::percent_format(accuracy = 1))


Related Topics



Leave a reply



Submit