Cyrus Mohammadian Cyrus Mohammadian - 3 months ago 15
R Question

manually add breaks to both x and y axis in ggplot2

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.

Answer
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