2015-12-30 10 views
5

Questa domanda riguarda generalmente un precedente SO question riguardante la creazione di segni di graduazione minori su un asse ggplot2 e in particolare su un comment nella risposta a tale domanda che suggerisce una funzione inserire spazi vuoti in una sequenza può rivelarsi utile.Inserisci spazi vuoti in un vettore per, ad esempio, piccole tacche in R

Come spesso aggiungo piccoli segni di graduazione a trame simili, ho provato a creare una tale funzione (vedere my answer di seguito).

risposta

12

La seguente funzione permette all'utente di richiedere che ogni nesimo elemento nth di un vettore x o essere (1) sostituito con un segnaposto carattere vuoto (empty = TRUE; predefinito) o (2) omessa dal vettore (empty = FALSE). Inoltre, offre la possibilità di richiedere l'inverso (inverse = TRUE, non predefinito) dell'operazione. La funzionalità è illustrata con alcuni esempi di seguito.

primo luogo, la funzione:

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

Alcuni esempi di sostituzione o omettendo elementi del vettore:

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" 

Tuttavia, per la creazione di zecche minori, si preferisce restituire l'inverso di questo operazione utilizzando l'opzione 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" 

Per illustrare l'uso della funzione per la creazione di zecche minori:

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 

default labelling

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

custom labelling

+0

Questo è incredibilmente utile. Spero che questo venga incorporato in ggplot un giorno! Risolto il mio problema – Nova

Problemi correlati