10

Problem: I cannot find any way to combine the breaks and limits commands in ggplot2. The y-axis should always contain the range of 0-40 and breaks=c(5,10,15,20,25,30,35). The x-axis should be 0-100, breaks=c(10,20,30,40,50,60,70,80,90,100). I do NOT want to display data that is outside this range.

I tried + ylim, but this overwrites my breaks. I tried + expand, but this also shows data outside the range that I want(1-100). I tried both adding the breaks and limiting the range in a second step, but the y-axis of my first step is simply overwritten if I do that.

plot_Tili_Age_VS_Height <- ggplot(Tili, aes(x = Age, y = Height)) + geom_point() + geom_smooth(method = "lm", se = FALSE, color = "black", formula = y ~ x) + scale_y_continuous(trans = "log10", breaks = c(5, 10, 15, 20, 25, 30, 35)) + expand_limits(y = c(0, 35), x = c(0, 100)) + scale_x_continuous(trans = "log10", breaks = c(10, 20, 30, 40, 50, 60,70, 80, 90, 100)) + theme_bw(base_size = 15) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) df <- data.frame(x = log(Tili$Age), y = log(Tili$Height)) lm_eqn = function(df) { m = lm(y ~ x, df) eq <- substitute(ln(italic(y)) == a + b %*% ln(italic(x)) * "," ~ ~italic(r)^2 ~ "=" ~ r2, list(a = format(coef(m)[1], digits = 2), b = format(coef(m)[2], digits = 2), r2 = format(summary(m)$r.squared, digits = 2))) as.character(as.expression(eq)) } plot_Tili_Age_VS_Height <- plot_Tili_Age_VS_Height + annotate("text", x = 30, y = 5, label = lm_eqn(df), hjust = 0, size = 3, family = "Times", parse = TRUE) plot_Tili_Age_VS_Height 

Any idea how to fix it?

6
  • 1
    Is Tili a built in dataset? Can you make this reproducible for others? Commented Jul 11, 2016 at 17:56
  • 1
    Have you tried using the limits argument in, e.g., scale_x_continuous? Commented Jul 11, 2016 at 17:59
  • Get rid of expand_limits(), don't use ylim or xlim, just use scale_y_continuous and scale_x_continuous, they take both breaks and limits arguments. Commented Jul 11, 2016 at 18:12
  • @JasonAizkalns I tried making a reproducible example but my code didn't work anymore with other data. Sorry for that! :( However, I think it doesn't really matter in my example what the data are, the problem is that the ggplot grammar does not let me combine limits = and breaks =, so I figured it should be okay without data Commented Jul 12, 2016 at 12:15
  • @aosmith Yes I did. It also overwrites the breaks. Commented Jul 12, 2016 at 12:16

1 Answer 1

17

As JasonAizkalns commented your problem can't be solved without n reproducible example. The code below does what you want on the iris data and should work for your example as well.

library(ggplot2) df <- iris ## all data, default breaks ggplot(df, aes(Sepal.Length, Sepal.Width)) + geom_point() ## subset of data is seen in plot, breaks changed ggplot(df, aes(Sepal.Length, Sepal.Width)) + geom_point() + scale_x_continuous(breaks = c(5.5,6.5), limits = c(5,7)) + scale_y_continuous(breaks = c(3.5,2.5), limits = c(2,4)) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you, suddenly it worked to combine breaks and limits, even though I ran the same code as yesterday (and had restarted the programme in between). Possible restarting the computer did the trick. So scale_x_continuous(breaks = c(5.5,6.5), limits = c(5,7)) was the solution! What you may have to keep in mind if you use log transformed data like me is that if you simply put c(0,40) or similar, your data may appear very small as the distance from 0 to the first break (10 in my case) is large and it could be better to use the real bottom limit of your data (4.4 in my case).
How would this work when editing the y-axis to display a log 10 scale? I am specifiying trans, breaks, and labels, but also want to slightly increase the y-axis range from what it is plotting on it's own. I want my y-axis to span from 0.5 to 3.5 (in log10). How can I incorporate limits into this chunk of code? scale_y_continuous(trans = log10_trans(), breaks = trans_breaks('log10', function(x) 10^x), labels = trans_format('log10', math_format(.x)))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.