Skip to content
Snippets Groups Projects
Commit 00a8bbcf authored by Deepankar Chakroborty's avatar Deepankar Chakroborty
Browse files

add script to calculate breaks for axes in ggplot2

calculates breaks and labels for axes in ggplot2 with user defined gaps
parent 21c9b5fd
No related branches found
No related tags found
No related merge requests found
# #<---------------------------->
# # Please include this section when distributing and/or using this code.
# # Please read and abide by the terms of the included LICENSE
# #
# # Author : Deepankar Chakroborty (https://gitlab.utu.fi/deecha)
# # Report issues: https://gitlab.utu.fi/deecha/shared_scripts/-/issues
# # License: https://gitlab.utu.fi/deecha/shared_scripts/-/blob/master/LICENSE
# #
# # PURPOSE:
# # Returns a list of vectors containing breaks and labels
# # for a continuous variable mapped to one of the axes for use with ggplot2
# # User enters:
# # - the range of the data
# # - the tick amount
# # - skip.steps (if any) = number of labels to skip
# # (i.e. show tick but no label)
# #
# #<---------------------------->
ggplotBreaks <- function(range,tick,skip.steps=0){
if (length(range) != 2){
stop("Correct format for: range = c(min_value,max_value)")
}
if(skip.steps<0){
stop(" 'skip.steps' should be >= 0")
}
breaks <- seq(from = range[1],
to = range[2],
by = tick)
if(skip.steps==0){
labels <- as.character(breaks)
} else {
tmp <- c()
labels <- unlist(lapply(breaks[seq(from = 1,to = length(breaks),by = skip.steps)],function(x) c(tmp,c(x,rep(" ",skip.steps-1)))),use.names = F)
rm(tmp)
}
return(list( breaks = breaks,
labels = labels))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment