Home > Enterprise >  How to log 10 scale axis in ggscatter
How to log 10 scale axis in ggscatter

Time:01-01

I am trying to plot a similar figure.

enter image description here

data(cars)
p <- ggscatter(cars, x = "speed", y = "dist")
p   yscale("log10", .format = TRUE)

I wanna use log10 to scale the axis, and then change the label. How to make a figure as above? enter image description here

CodePudding user response:

If you are not satisfied by p yscale("log10", .format = TRUE), you can also set the breaks manually:

p   scale_y_log10(breaks = c(1,10,100), limits = c(1,120))

enter image description here

CodePudding user response:

If you're just trying to change the formatting of the labels, scales::number_format() may be a good option. You will also need to adjust the limits to include the breaks you want to see if they're outside the range of the data. In your example 1 won't show up if you don't extend the limits because the axis limits would be clipped closer to the data range by default.

library(tidyverse)
library(ggpubr)
library(scales)

data(cars)
p <- ggscatter(cars, x = "speed", y = "dist")
p   scale_y_log10(labels = number_format(accuracy = 1), 
                  breaks = 10^(0:2),
                  limits = c(1, 100))

Created on 2021-12-31 by the reprex package (v2.0.1)

  • Related