4

I've typically changed the scale breaks for continuous variables in ggplot2 using scale_y_continuous (or scale_x_continuous). I also usually set the axis limits using coord_cartesian(xlim=) (or ylim= for y axis). All that works just fine like in the following:

#Some example data set.seed(100) b<-sample(1:10, 10) a<-sample(1:10, 10) df<-data.frame(a,b) #Graph without scales set (just default) library(ggplot2) ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df)), color="black") 

That produces the following plot with default scales:

enter image description here

If I want to adjust the x-axis to, say, 1-13 (i.e. 13 tick marks starting at 1 and ending at 13) I would do the following:

ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df)), color="black")+ scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+ coord_cartesian(xlim=c(0, 13)) 

enter image description here

...and for the y-axis...

ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df)), color="black")+ scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+ coord_cartesian(ylim=c(0, 13)) 

enter image description here

However, when I try to accomplish the very same scale adjustment for both axes at the same time, the output does not produce what I would expect (which is 1-13 on both the x and y axes).

ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df)), color="black")+ scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+ coord_cartesian(ylim=c(0, 13))+ scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+ coord_cartesian(xlim=c(0, 13)) 

enter image description here

As you can see, the x and y axis are not equivalent even though the exact same code has been passed to both axes. I don't understand why. Any help is much appreciated.

1
  • 1
    not sure but I forgot to add one line of code (my bad), just the data frame to gather a and b: df<-data.frame(a,b) I've fixed it in the example above Commented Aug 26, 2016 at 2:47

2 Answers 2

7
set.seed(100) df <- data.frame(b = sample(1:10, 10), a = sample(1:10, 10)) ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) + geom_text(aes(label=rownames(df)), color="black") + scale_y_continuous(breaks = c(1,3,5,7,9,11,13)) + scale_x_continuous(breaks = c(1,3,5,7,9,11,13)) + coord_fixed(ylim=c(0, 13),xlim=c(0, 13)) 

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

ahh i see its coord_fixed Thanks!
3

scale_*_continuous functions have a limits parameter you can use instead of coord_cartesian:

ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df)), color="black")+ scale_y_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13)) + scale_x_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13)) 

plot with proper limits

...or if you insist on doing it with coord_cartesian, do it in one call. Otherwise it crops your graph and then crops it again, which is what is causing the problem.

ggplot(data=df, aes(a,b))+ theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+ geom_text(aes(label=rownames(df)), color="black")+ scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+ scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+ coord_cartesian(xlim=c(0, 13), ylim = c(0, 13)) # returns the same thing 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.