将空白插入矢量,例如 R 中的小刻度标签
Insert blanks into a vector for, e.g., minor tick labels in R
这个问题通常与之前 SO question concerning the creation of minor tick marks on a ggplot2
axis and specifically to a comment 问题的答案有关,建议将空格插入序列的函数可能会很有用。
由于我经常在类似的图中添加小的刻度线,所以我试着创建了这样一个函数(参见下面的 )。
以下函数允许用户请求每个 nth 个元素 nth
向量 x
可以 (1) 替换为空字符占位符(empty = TRUE
;默认)或 (2) 从向量中省略 (empty = FALSE
)。此外,它还提供了请求操作的逆操作(inverse = TRUE
;不是默认值)的选项。下面的一些示例说明了该功能。
一、功能:
every_nth <- function(x, nth, empty = TRUE, inverse = FALSE)
{
if (!inverse) {
if(empty) {
x[1:nth == 1] <- ""
x
} else {
x[1:nth != 1]
}
} else {
if(empty) {
x[1:nth != 1] <- ""
x
} else {
x[1:nth == 1]
}
}
}
替换或省略矢量元素的一些示例:
numvec <- 0:20
charvec <- LETTERS
## Replace every 3rd element with an empty character
every_nth(numvec, 3) # conversion to character vector
[1] "" "1" "2" "" "4" "5" "" "7" "8" "" "10" "11" "" "13"
[15] "14" "" "16" "17" "" "19" "20"
every_nth(charvec, 3)
[1] "" "B" "C" "" "E" "F" "" "H" "I" "" "K" "L" "" "N" "O" "" "Q"
[18] "R" "" "T" "U" "" "W" "X" "" "Z"
## Omit (drop) every 3rd element
every_nth(numvec, 3, empty = FALSE) # vector mode is preserved
[1] 1 2 4 5 7 8 10 11 13 14 16 17 19 20
every_nth(charvec, 3, empty = FALSE)
[1] "B" "C" "E" "F" "H" "I" "K" "L" "N" "O" "Q" "R" "T" "U" "W" "X" "Z"
但是,对于创建次要刻度,最好是 return 使用 inverse = TRUE
选项的逆操作:
## Retain every 3rd element, replacing all others with an empty character
every_nth(numvec, 3, inverse = TRUE) # conversion to character vector
[1] "0" "" "" "3" "" "" "6" "" "" "9" "" "" "12" ""
[15] "" "15" "" "" "18" "" ""
every_nth(charvec, 3, inverse = TRUE)
[1] "A" "" "" "D" "" "" "G" "" "" "J" "" "" "M" "" "" "P" ""
[18] "" "S" "" "" "V" "" "" "Y" ""
## Retain every 3rd element, omitting (dropping) all other elements
every_nth(numvec, 3, empty = FALSE, inverse = TRUE) # vector mode is preserved
[1] 0 3 6 9 12 15 18
every_nth(charvec, 3, empty = FALSE, inverse = TRUE)
[1] "A" "D" "G" "J" "M" "P" "S" "V" "Y"
为了说明函数在创建小刻度中的用途:
library(ggplot2)
df <- data.frame(x = rnorm(1000), y = rnorm(1000))
## ggplot2 default axis labelling
p <- ggplot(df, aes(x, y)) + geom_point() + theme_bw()
p
## Add minor ticks to axes
custom_breaks <- seq(-3, 3, 0.25)
p +
scale_x_continuous(breaks = custom_breaks,
labels = every_nth(custom_breaks, 4, inverse = TRUE)) +
scale_y_continuous(breaks = custom_breaks,
labels = every_nth(custom_breaks, 2, inverse = TRUE))
这个问题通常与之前 SO question concerning the creation of minor tick marks on a ggplot2
axis and specifically to a comment 问题的答案有关,建议将空格插入序列的函数可能会很有用。
由于我经常在类似的图中添加小的刻度线,所以我试着创建了这样一个函数(参见下面的
以下函数允许用户请求每个 nth 个元素 nth
向量 x
可以 (1) 替换为空字符占位符(empty = TRUE
;默认)或 (2) 从向量中省略 (empty = FALSE
)。此外,它还提供了请求操作的逆操作(inverse = TRUE
;不是默认值)的选项。下面的一些示例说明了该功能。
一、功能:
every_nth <- function(x, nth, empty = TRUE, inverse = FALSE)
{
if (!inverse) {
if(empty) {
x[1:nth == 1] <- ""
x
} else {
x[1:nth != 1]
}
} else {
if(empty) {
x[1:nth != 1] <- ""
x
} else {
x[1:nth == 1]
}
}
}
替换或省略矢量元素的一些示例:
numvec <- 0:20
charvec <- LETTERS
## Replace every 3rd element with an empty character
every_nth(numvec, 3) # conversion to character vector
[1] "" "1" "2" "" "4" "5" "" "7" "8" "" "10" "11" "" "13"
[15] "14" "" "16" "17" "" "19" "20"
every_nth(charvec, 3)
[1] "" "B" "C" "" "E" "F" "" "H" "I" "" "K" "L" "" "N" "O" "" "Q"
[18] "R" "" "T" "U" "" "W" "X" "" "Z"
## Omit (drop) every 3rd element
every_nth(numvec, 3, empty = FALSE) # vector mode is preserved
[1] 1 2 4 5 7 8 10 11 13 14 16 17 19 20
every_nth(charvec, 3, empty = FALSE)
[1] "B" "C" "E" "F" "H" "I" "K" "L" "N" "O" "Q" "R" "T" "U" "W" "X" "Z"
但是,对于创建次要刻度,最好是 return 使用 inverse = TRUE
选项的逆操作:
## Retain every 3rd element, replacing all others with an empty character
every_nth(numvec, 3, inverse = TRUE) # conversion to character vector
[1] "0" "" "" "3" "" "" "6" "" "" "9" "" "" "12" ""
[15] "" "15" "" "" "18" "" ""
every_nth(charvec, 3, inverse = TRUE)
[1] "A" "" "" "D" "" "" "G" "" "" "J" "" "" "M" "" "" "P" ""
[18] "" "S" "" "" "V" "" "" "Y" ""
## Retain every 3rd element, omitting (dropping) all other elements
every_nth(numvec, 3, empty = FALSE, inverse = TRUE) # vector mode is preserved
[1] 0 3 6 9 12 15 18
every_nth(charvec, 3, empty = FALSE, inverse = TRUE)
[1] "A" "D" "G" "J" "M" "P" "S" "V" "Y"
为了说明函数在创建小刻度中的用途:
library(ggplot2)
df <- data.frame(x = rnorm(1000), y = rnorm(1000))
## ggplot2 default axis labelling
p <- ggplot(df, aes(x, y)) + geom_point() + theme_bw()
p
## Add minor ticks to axes
custom_breaks <- seq(-3, 3, 0.25)
p +
scale_x_continuous(breaks = custom_breaks,
labels = every_nth(custom_breaks, 4, inverse = TRUE)) +
scale_y_continuous(breaks = custom_breaks,
labels = every_nth(custom_breaks, 2, inverse = TRUE))