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)
R, ggplot: Decimals on y-axis
With the new version of scales
you can use:
scale_y_continuous(labels = scales::percent_format(accuracy = 1))
Scaling axis with different decimal places in ggplot
Just set the labels=
as well. You can put whatever value you want there.
scale_x_continuous("",
limits = c(0.1, 10),
breaks = c( .1, .2, .4,.67, .8, 1, 1.25, 1.5, 2.5, 5, 10),
labels = c( ".1", ".2", ".4" , ".67", ".8", "1", "1.25", "1.5", "2.5", "5", "10"),
)
ggplot - logarithmic axis labels with defined decimal places
The math_format()
function has a format
argument wherein you can stack another function to format your numbers.
library(ggplot2)
library(scales)
dummy <- data.frame(
x = "A",
y = 10^runif(100, min = 1, max = 2.5)
)
ggplot(data = dummy, aes(x = x, y = y)) +
geom_boxplot() +
scale_y_continuous(
trans = log10_trans(),
breaks = trans_breaks("log10", function(x) 10^x),
labels = math_format(
format = function(x){number(log10(x), accuracy = 0.1)}
)
) +
theme(
text = element_text(size = 16)
)
Created on 2021-09-09 by the reprex package (v2.0.1)
Is there an option to set , as default decimal point for axis labels in ggplot?
The key seems to be that format
(from base R) and scales::number
use different rules. We can revert to using format
...
myf <- function(x, ...) format(x, big.mark = ".", decimal.mark = ",", ...)
ggplot(mtcars, aes(drat, mpg)) +
geom_point() +
scale_x_continuous(labels = myf)
If you want to make these labels the global default I think you can do this:
scale_x_continuous <- function(..., labels = myf) {
do.call(ggplot2::scale_x_continuous, c(list(...), labels = labels))
}
How do I adjust the decimal places of a ggplot axis label?
You can specify this with a function inside scale_y_continuous
with the breaks
argument:
A %>%
drop_na(Roadgroup) %>%
drop_na(L.Share) %>%
filter(L.Share != "#DIV/0!") %>%
ggplot(aes(x=LP.share, y=L.Share, colour=Ward)) +
geom_point() +
scale_y_continuous(breaks = function(y) seq(floor(min(y, digits = 1)),
ceiling(max(y, digits = 1)),
by = 0.05))
You may need to modify my function(y)
here to fit your needs (by changing digits
, and by
). However, if A$L.Share
is a factor, you will need to convert this to a numeric in order for scale_y_continuous
to work properly.
Formatting numbers on axis when using ggplot2
Exploiting the fact that you can give a (lambda) function as the labels
argument, you can just reconvert the character label to numeric before passing it on to scales::number
.
library(ggplot2)
library(scales)
library(dplyr)
RN <- sample(1:1000,1000,replace=TRUE)
RN <- RN/1000
breaks <- c(seq(from=0, to=1, by=0.05))
DF <- data.frame(RN)
DF$DisRN <- cut(DF$RN,breaks=c(breaks,Inf),labels=as.numeric(breaks))
DF_Plot <- DF %>% group_by(DisRN) %>% summarise(cnt=n())
ggplot(DF_Plot,aes(y=cnt,x=DisRN)) +
geom_col(position="dodge") +
scale_x_discrete(
labels = ~ number(as.numeric(.x), accuracy = 0.01)
)
Created on 2022-01-11 by the reprex package (v2.0.1)
You can leave out some breaks by setting the breaks
argument of the scale to, for example, breaks = seq(0, 0.9, by = 0.1)
.
Remove decimals y axis ggplot2
Use percent_format
from the scales
package to set accuracy
to 1.
library(ggplot2)
library(scales)
ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
scale_y_continuous(labels = percent_format(accuracy = 1)) +
geom_bar(stat = "identity") +
theme_bw() +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "none") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
theme(panel.grid.major = element_blank())
DATA
df_Filtered <- read.table(text = "Product Relative_Value
Car 0.12651458
Plane 0.08888552
Tank 0.03546231
Bike 0.06711630
Train 0.06382191",
header = TRUE, stringsAsFactors = FALSE)
Related Topics
Combining New Lines and Italics in Facet Labels with Ggplot2
Combining Duplicated Rows in R and Adding New Column Containing Ids of Duplicates
Mutate Multiple Variable to Create Multiple New Variables
Strange Formatting of Legend in Ggplotly in R
Submit Form with No Submit Button in Rvest
Ggplot2: Font Style in Label Expression
R: Sample() Command Subject to a Constraint
R * Not Meaningful for Factors Error
Row-Wise Sort Then Concatenate Across Specific Columns of Data Frame
Reading Psv (Pipe-Separated) File or String
Maps, Ggplot2, Fill by State Is Missing Certain Areas on the Map
R: Using a String as an Argument to Mutate Verb in Dplyr
Ggplot2: Reorder Bars from Highest to Lowest in Each Facet
Possible to Create Latex Multicolumns in Xtable
How to Create Vectors with Specific Intervals in R
Using R to Download Gzipped Data File, Extract, and Import Data