Skip to content

Latest commit

 

History

History
151 lines (103 loc) · 7.47 KB

r.md

File metadata and controls

151 lines (103 loc) · 7.47 KB

R

Base R

fahrenheit_to_celsius <- function(temp_F) {
  temp_C <- (temp_F - 32) * 5 / 9
  return(temp_C) 
}

note: return is optional; variable on last line is returned by default.

Tidyverse

  • define a factor: factor(colname, ordered = TRUE, levels = c('a', 'b', 'c'))
  • convert column to factor type (using current order of data as factor level order: df %>% mutate(colname = factor(colname, ordered = TRUE, levels = unique(colname))
  • General sorting of factors approach: numeric_levels <- sort(unique(numeric_vector)) numeric_factor_vector <- factor(numeric_vector, levels = numeric_levels)
  • Suppress package loading messages: suppresspackagestartupmessages({library(readxl) ...})
  • Read args from command line: args <- commandArgs(trailingOnly = TRUE) input_filename <- args[1] sheetname <- args[2]
  • How to reference a column names as a variable: mtcars %>% filter(get(var) == 4)

Non-equi joins

Tidyverse

  • Filter only if a condition

You could do

library(dplyr)
y <- ""
data.frame(x = 1:5) %>% 
  {if (y=="") filter(., x>3) else filter(., x<3)} %>% 
  tail(1)

or

data.frame(x = 1:5) %>% 
 filter(if (y=="") x>3 else x<3) %>%  
  tail(1)

or even store your pipe in the veins of

mypipe <- . %>% tail(1) %>% print
data.frame(x = 1:5) %>% mypipe

stackoverflow

GGPlot

  • Line plot for discrete x-axis:

    ggplot(data, aes(x=factor(year), y=dist, group=1)) + geom_line() + geom_point()

  • Hide aes legend ... + guides(color = FALSE, size = FALSE)

  • Hide geom legend ... + geom_point(..., show.legend = FALSE)

  • Scale axes ... + scale_y_continuous(name="Stopping distance", limits=c(0, 150))

  • linetype options: linetype = {"blank"|"solid"|"dashed"|"dotted"|"dotdash"|"longdash"|"twodash"}

  • add geom_text (label) with LaTeX formatting

    library(latex2exp) ... + geom_text(data, aes(x, y, label = TeX(colname, output='character')), parse = TRUE)

  • Color scales and palettes

    with palette brewer:

    scale_color_brewer(palette="Set1")

    with Wes Anderson themes:

    library(wesanderson) ... + scale_color_manual(values=wes_palette(n=3, name="GrandBudapest"))

    with Grayscale:

    scale_color_grey(start=0.8, end=0.2) # start and end are optional

    with Gradients:

    scale_fill_gradient(low="blue", high="red") # sequential gradient, 2 colors scale_color_gradient2(midpoint=mid, low="blue", mid="white", high="red", space ="Lab" ) # diverging gradient scale_color_gradientn(colours = rainbow(5)) # between n colors

  • Axis number format with commas (not scientific notation)

    without scientific notation:

    scale_y_log10(labels = function(n){format(n, scientific = FALSE)})

    with commas

    scale_y_log10(labels = scales::comma) scale_y_log10(labels = function(n){format(n, scientific = FALSE, big.mark = ",")})

    just 10^x

    scale_y_log10(labels = scales::trans_format("log10", scales::math_format(10^.x)))

  • How to remove padding space on axis: scale_x_continuous(limits = ..., expand = expansion(mult = c(0,0)))

  • Theme font size changes theme(axis.text.x = element_text(size = 12))

Useful Links

RStudio

  • Show all panes: Ctrl + Alt + Shift + 0