mirror of
https://github.com/dchakro/shared_Rscripts.git
synced 2026-05-17 09:12:16 -07:00
commit fe050d47c2dc9e61a13d4a362abf56dabfbecbd7
Author: Deepankar Chakroborty <deepankar.chakroborty@utu.fi>
Date: Fri Jul 31 12:57:11 2020 +0300
Include usage instruction inside function body
Include usage instruction inside function body, so on typing just the function name to display the R code, the instructions will appear.
Also stated things clearly in the T&C
commit fedbca4a7aa8fb436702accdf1f9b5ec7f066d60
Author: Deepankar Chakroborty <deepankar.chakroborty@utu.fi>
Date: Thu Jul 30 12:50:20 2020 +0300
Updates
1. Improve readability
2. Manage differing lengths of breaks and labels.
commit fb32fb9173c66c8862adb0ad41d82db9d128777b
Author: Deepankar Chakroborty <deepankar.chakroborty@utu.fi>
Date: Thu Jul 30 12:43:20 2020 +0300
skip.steps works as expected
skip.steps = 1, now skips 1 observation.
commit 00a8bbcf04731c010e0a5989a805274609560188
Author: Deepankar Chakroborty <deepankar.chakroborty@utu.fi>
Date: Thu Jul 30 12:32:16 2020 +0300
add script to calculate breaks for axes in ggplot2
calculates breaks and labels for axes in ggplot2 with user defined gaps
65 lines
2.2 KiB
R
65 lines
2.2 KiB
R
ggplotBreaks <- function(range,tick,skip.steps=0){
|
|
# #<---------------------------->
|
|
# # You must include this section when:
|
|
# # Distributing, Using and/or Modifying this code.
|
|
# # Please read and abide by the terms of the included LICENSE.
|
|
# # Copyright 2020, Deepankar Chakroborty, All rights reserved.
|
|
# #
|
|
# # 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)
|
|
# # e.g.
|
|
# # ggplotBreaks(c(0,150), tick =10, skip.steps = 1) # gives
|
|
# # $breaks
|
|
# # 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
|
|
# #
|
|
# # $labels
|
|
# # "0" " " "20" " " "40" " " "60" " " "80" " " "100" " " "120" " " "140" " "
|
|
|
|
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+1)],
|
|
function(x) c(tmp,
|
|
c(x,
|
|
rep(" ",skip.steps)))),
|
|
use.names = F)
|
|
rm(tmp)
|
|
|
|
}
|
|
if(length(labels) < length(breaks)){
|
|
## Add more spaces at the end to make lengths match
|
|
labels <- c( labels, rep(" " , (length(breaks) - length(labels))))
|
|
}
|
|
if ( length(breaks) < length(labels)){
|
|
## Remove spaces from the end to make the lengths match
|
|
labels <- labels[1:length(breaks)]
|
|
}
|
|
return(list( breaks = breaks,
|
|
labels = labels))
|
|
}
|