Reputation: 1440
The last line in the following code sets the origin of the plot at zero for the y-scale and the y-scale's limits. How do I do this for the x-scale when using a log10 grid?
breaks <- 10^(-10:10) # break for major axis
minor_breaks <- rep(1:9, 21)*(10^rep(-10:10, each = 9)) # breaks for minor axis
ggplot(data = cars, aes(x = dist, y = speed)) +
geom_point() +
scale_x_log10(breaks = breaks, minor_breaks = minor_breaks) + # use log10 scale for x
theme(panel.grid.major = element_line(colour = "grey50")) +
theme(panel.grid.minor = element_line(colour = "grey70")) +
theme_light(base_size = 12) +
scale_y_continuous(expand = c(0, 0), limits = c(0,30)) # sets origin and limits for y-scale
Upvotes: 1
Views: 647
Reputation: 66835
I suggest using a modified transformation, pseudo_log_trans
, which accommodates zero values (and even negatives) by smoothly transitioning between a linear scale in [-1, 1] and a signed log scale beyond that range. The approximate point of transition can be modified using the first parameter, sigma
.
ggplot(data = cars, aes(x = dist, y = speed)) +
geom_point() +
scale_x_continuous(trans = scales::pseudo_log_trans(1)) +
theme(panel.grid.major = element_line(colour = "grey50")) +
theme(panel.grid.minor = element_line(colour = "grey70")) +
theme_light(base_size = 12) +
coord_cartesian(expand = FALSE, # EDIT #2
xlim = c(0, 130), ylim = c(0, 30))
Here's how it transitions between the linear and log scales:
plot(scales::pseudo_log_trans(), xlim = c(-5, 10))
lines(scales::log_trans(), xlim = c(-5, 10), col = "red")
Upvotes: 3