How to Put Exact Number of Decimal Places on Label Ggplot Bar Chart

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

How to round off data labels for a bar plot in geom_text in ggplot2?

Just add accuracy = 1 within scales::percent function like

ggplot(iris, aes(x=Sepal.Width))+
geom_bar(color="Blue", fill="skyblue")+
geom_text(stat="count", aes(label=scales::percent(..prop.., accuracy = 1), group=1), size =3, vjust=-0.3)+
ylab("Count of Suppliers")+
xlab("D&B Score of Suppliers")+
ggtitle("D&B Score distribution of suppliers")+
theme_classic()

Sample Image

To have 1 or 2 decimals you can use accuracy = 0.1 or accuracy = 0.01. For details visit this.

How to control decimal places for value labels in bar charts

The problem is that you rounded (and thus converted to character, losing numeric information) before using sprintf. To format numbers, you need to give sprintf numbers, not strings. It will take care of the rounding itself. Try this:

label = ifelse(value !=0, sprintf("%0.1f", value), "")

Making the whole code:

ggplot(data = df, aes(y = value, x = group, fill = cost)) +
geom_bar(stat = "identity", position = 'stack') +
ylab("Y label") +
theme(
legend.direction = "horizontal",
legend.position = "bottom",
legend.spacing.x = unit(0.1, 'cm')
) +
theme(legend.title = element_blank()) +
geom_text(aes(label = ifelse(value !=0, sprintf("%0.1f", value), "")),
position = position_stack(vjust = 0.5)) +
facet_grid(~ treatment)

Sample Image


The above is a little weird because of the ifelse. A more standard ggplot2 solution would have you get rid of the 0s another way - maybe filter it out before plotting, give data = filter(df, y != 0) to ggplot(). Then you can use the scales functions

label = scales::label_number(accuracy = 0.1)(value)

Making the whole code as below, for the same result:

ggplot(data = dplyr::filter(df, value != 0), aes(y = value, x = group, fill = cost)) +
geom_bar(stat = "identity", position = 'stack') +
ylab("Y label") +
theme(
legend.direction = "horizontal",
legend.position = "bottom",
legend.spacing.x = unit(0.1, 'cm')
) +
theme(legend.title = element_blank()) +
geom_text(aes(label = scales::label_number(accuracy = 0.1)(value)),
position = position_stack(vjust = 0.5)) +
facet_grid(~ treatment)

How do I change the number of decimal places on axis labels in ggplot2?

From the help for ?scale_y_continuous, the argument 'labels' can be a function:

labels One of:

  • NULL for no labels

  • waiver() for the default labels computed by the transformation object

  • A character vector giving labels (must be same length as breaks)

  • A function that takes the breaks as input and returns labels as output

We will use the last option, a function that takes breaks as an argument and returns a number with 2 decimal places.

#Our transformation function
scaleFUN <- function(x) sprintf("%.2f", x)

#Plot
library(ggplot2)
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p <- p + facet_grid(. ~ cyl)
p + scale_y_continuous(labels=scaleFUN)

Sample Image

Show rounded decimal places as label in ggplot2

When you are parsing an expression, put single quotes around the %.2f in your sprintf string to show a fixed number of digits:

p1 + 
geom_vline(aes(xintercept = 1), size = .5, linetype = 'dashed') +
geom_errorbarh(aes(xmax = ifelse(upr.ci < 13, upr.ci, 13), xmin = lwr.ci),
size = 2, height = 0, color = 'gray') +
geom_point(size = 4, color = 'black') +
theme(panel.grid = element_blank()) +
scale_x_continuous(breaks = c(0:13), limits = c(-3,13)) +
ylab('CL') +
xlab('Odds Ratios') +
ggtitle('title') +
geom_text(aes(label = paste(sprintf("'%.2f'", or), " (", "italic(n)==", n ,")")),
x= -1.5, size = 8, color = 'black', family = "Times New Roman",
parse = TRUE) +
geom_text(aes(label = plab), vjust = -0.5, fontface = 'bold', size = 8)

Sample Image

Force bars/rows in stacked bar chart (ggplot showing percent, zero decimal places) to be of same lenght?

A very simple solution to your problem is to change from position = position_stack() to position = position_fill().

library(tidyverse)
library(scales)

# Creating some data
df <- data.frame(matrix(floor(runif(12*7200, 1, 6)), ncol = 12, dimnames = list(NULL, paste0("item", 1:12))))

# Drawing the ggplot with 0 (zero) decimal places
df %>%
gather %>%
group_by(key, value) %>%
tally %>%
mutate(n = round(n/sum(n) * 100, 0)) %>%
ggplot(aes(x = key, y = n, fill = as.factor(value))) +
geom_col(position = position_fill(reverse = T)) +
labs(title = "Some title", x = " ", fill = " ") +
scale_y_continuous(name = "%", labels = percent) +
geom_text(aes(label = n), position = position_fill(reverse = TRUE, vjust = 0.5), size = 3, colour = "white") +
theme(legend.position = "top") +
coord_flip() +
theme_minimal()

Sample Image

Created on 2022-04-06 by the reprex package (v2.0.1)

Limit the decimals in histogram labels

You can pass "accuracy" to scales::percent, e.g.

library(scales)
count = seq(0, 10, 1)
label = scales::percent(cumsum((count / sum(count))), accuracy = 0.001)
label
>[1] "0.000%" "1.818%" "5.455%" "10.909%" "18.182%"
>[6] "27.273%" "38.182%" "50.909%" "65.455%" "81.818%"
>[11] "100.000%"

label = scales::percent(cumsum((count / sum(count))), accuracy = 0.01)
label
>[1] "0.00%" "1.82%" "5.45%" "10.91%" "18.18%"
>[6] "27.27%" "38.18%" "50.91%" "65.45%" "81.82%"
>[11] "100.00%"

So, with your data, I think this should work:

label =  after_stat(scales::percent(cumsum((count / sum(count))), accuracy = 0.01))


Related Topics



Leave a reply



Submit