将空白插入向量中,例如 R 中的次要刻度标签 [英] Insert blanks into a vector for, e.g., minor tick labels in R

查看:16
本文介绍了将空白插入向量中,例如 R 中的次要刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题一般与之前的SO 问题 关于在 ggplot2 轴上创建小刻度线,特别是 comment20194683_14490652">评论 建议将空白插入序列的函数可能证明有用.

This question relates generally to a previous SO question concerning the creation of minor tick marks on a ggplot2 axis and specifically to a comment in the answer to that question suggesting a function to insert blanks into a sequence may prove useful.

因为我经常在类似的图中添加小刻度线,所以我尝试创建这样一个函数(参见 我的回答 下面).

As I frequently add minor tick marks to similar plots, I took a stab at creating such a function (see my answer below).

推荐答案

下面的函数允许用户请求每个nth元素nthx 的 code> 要么被 (1) 替换为空字符占位符 (empty = TRUE; 默认) 或 (2) 从向量中省略 (空 = FALSE).此外,它还提供了请求操作的逆(inverse = TRUE;不是默认值)的选项.下面通过一些示例说明了该功能.

The following function allows the user to request that every nth element nth of a vector x either be (1) replaced with a empty character placeholder (empty = TRUE; default) or (2) omitted from the vector (empty = FALSE). Additionally, it provides the option of requesting the inverse (inverse = TRUE; not default) of the operation. The functionality is illustrated with some examples below.

一、功能:

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]
        }
    }
}

替换或省略向量元素的一些示例:

Some examples of replacing or omitting vector elements:

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"

但是,为了创建小刻度,最好使用 inverse = TRUE 选项返回此操作的反函数:

However, for the creation of minor ticks, it is preferred to return the inverse of this operation using the inverse = TRUE option:

## 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"

为了说明该函数在创建小刻度时的使用:

To illustrate the function's use in the creation of minor ticks:

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)) 

这篇关于将空白插入向量中,例如 R 中的次要刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆