Change axis breaks without defining sequence - ggplot

Change axis breaks without defining sequence - ggplot



Is there any way to set the break step size in ggplot without defining a sequence. For example:


x <- 1:10
y <- 1:10

df <- data.frame(x, y)

# Plot with auto scale
ggplot(df, aes(x,y)) + geom_point()

# Plot with breaks defined by sequence
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(0,10,1))

# Plot with automatic sequence for breaks
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(min(df$y),max(df$y),1))

# Does this exist?
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(break_step = 1)



You may say I am being lazy but there have been a few occasions where I have had to change the min and max limits of my seq due to the addition of error bars. So I just want to say...use a break size of x, with automatic scale limits.


min


max


seq




1 Answer
1



You can define your own function to pass to the breaks argument. An example that would work in your case would be


f <- function(y) seq(floor(min(y)), ceiling(max(y)))



Then


ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f)



gives



enter image description here



You could modify this to pass the step of the breaks, e.g.


f <- function(k)
step <- k
function(y) seq(floor(min(y)), ceiling(max(y)), by = step)



then


ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f(2))



would create a y-axis with ticks at 2, 4, .., 10, etc.



You can take this even further by writing your own scale function


my_scale <- function(step = 1, ...) scale_y_continuous(breaks = f(step), ...)



and just call it like


ggplot(df, aes(x,y)) + geom_point() + my_scale()





Nice one, that worked great. Thanks
– Pete900
Aug 19 '16 at 14:58





This was HUGE for me
– papelr
Jul 14 '18 at 22:09



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế