Pretty Axis Labels for Log Scale in Ggplot

Pretty axis labels for log scale in ggplot

You can use trans_breaks() and trans_format() from library scales to get desired formatting of axis values.

library(scales)
qplot(1:10, 10^(1:10)) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))

Sample Image

ggplot: Log scale with linear labels

Try trans inside scale_x_contineous, which transform axis only.

ggplot(data, aes(parties, poverty))+
geom_point(size=2)+
scale_x_continuous(
trans = "log10",
breaks = 1:10
)

Log axis labels in ggplot2: Show only necessary digits?

Add drop0trailing = TRUE in plain.

plain <- function(x,...) {
format(x, ..., scientific = FALSE, drop0trailing = TRUE)
}

To see other options for pretty printing, see ?format.

Sample Image

ggplot2 axis: set intervals, logarithmic scale, and exponents instead of scientific

Here are some guidance (I'm using tidyr::population dataset)

1.


library(ggplot2)
library(scales)
library(tidyr)
ggplot(population, aes(country, population)) +
geom_bar(aes(fill=year),stat="identity",position="dodge") +
scale_y_continuous(breaks = pretty_breaks(n = 10))

Sample Image

2.


library(ggplot2)
library(scales)
library(tidyr)
ggplot(population, aes(country, population)) +
geom_bar(aes(fill=year),stat="identity",position="dodge") +
scale_y_log10()

Sample Image

putting it all together:



library(ggplot2)
library(scales)
library(tidyr)
ggplot(population, aes(country, population)) +
geom_bar(aes(fill=year),stat="identity",position="dodge") +
scale_y_log10(labels= trans_format(log10, math_format(10^.x)),
breaks =trans_breaks(log10, function(x) 10^x, 10))

Sample Image

Labelling logarithmic scale display in R

Apart from the solution of ggplot2 (see gsk3's comment), I would like to add that this happens automatically in plot() as well when using the correct arguments, eg :

x <- 1:10
y <- exp(1:10)
plot(x,y,log="y")

You can use the parameter log="x" for the X axis, or log="xy" for both.

If you want to format the numbers, or you have the data in log format, you can do a workaround using axis(). Some interesting functions :

  • axTicks(x) gives you the location of the ticks on the X-axis (x=1) or Y-axis (x=2)
  • bquote() converts expressions to language, but can replace a variable with its value. More information on bquote() in the question Latex and variables in plot label in R? .
  • as.expression() makes the language object coming from bquote() an expression. This allows axis() to do the formatting as explained in ?plotmath. It can't do so with language objects.

An example for nice formatting :

x <- y <- 1:10
plot(x,y,yaxt="n")
aty <- axTicks(2)
labels <- sapply(aty,function(i)
as.expression(bquote(10^ .(i)))
)
axis(2,at=aty,labels=labels)

Which gives

Sample Image

Data Labels for Quantiles on an Inverse Hyperbolic Sine Scale in R (ggplot)

It may be simpler to put your quantile labels in a separate dataframe prior to plotting, then pass the quantile dataframe to the data argument of geom_text:

library(tidyverse)
library(scales)

XDataFiveNum <- XData %>%
group_by(SensorLocation, Setup) %>%
summarize(Strain = fivenum(Strain), .groups = "drop")

NPlot <- ggplot(XData, aes(fill = `Setup`, x = `SensorLocation`, y = `Strain`)) +
geom_violin(trim = TRUE, fill = "lightgray") +
labs(x = "Sensor Location", y = "Strain (\u03BC\u03B5)\n- inverse hyperbolic sine scale") +
geom_boxplot(width=0.2) +
geom_text(
data = XDataFiveNum,
aes(label = sprintf("%.1f", Strain), color = Setup),
position=position_nudge(x=0.33),
size=3.5
) +
theme_bw() +
#coord_cartesian(ylim = quantile(XData$Bstrain, c(0, 1))) +
scale_y_continuous(trans = asinh_trans, breaks = c(-1000, -100, -10, -1, -0.1)) +
theme(axis.title = element_text(size = 12)) +
theme(axis.text = element_text(size = 12, color = "black")) +
theme(axis.title.x = element_text(vjust = -3)) +
theme(axis.text.x = element_text(vjust = -1.5)) +
theme(panel.grid.major = element_line(size = 0.5, linetype = 'dashed', color = "dark grey"),
panel.grid.minor = element_line(size = 0.5, linetype = 'dashed', color = "grey"),
panel.background = element_rect(colour = "black", size=1)) +
theme(legend.position = "bottom") +
guides(fill=guide_legend(title="Test Setup"),
colour = guide_legend(title="Test Setup"))

R ggplot scale_y_log10, remove 10^(-0.5) from log scale

You could try playing with the n argument to scales::breaks_log (the default is n=5), as in

scale_y_log10(breaks = breaks_log(n=3))

Or if you are willing to hard-code the solution for a particular graph you could use

scale_y_log10(breaks = 10^(1:3))

once you've established the range you want.



Related Topics



Leave a reply



Submit