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.
- 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)
-
OPEN GITHUB ISSUE: join_by(): Syntax for generic joins · Issue #2240 · tidyverse/dplyr
-
One guys approach: In between a rock and a conditional join
-
Another guys approach: Thought process to choose a Tidyverse approach: join, mutate + map, or other
-
Fuzzy join: dgrtwo/fuzzyjoin: Join tables together on inexact matching
-
TEASED IN 2015 FOR DPLYR: dplyr 0.4.0
- 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
-
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 formattinglibrary(latex2exp) ... + geom_text(data, aes(x, y, label = TeX(colname, output='character')), parse = TRUE)
-
scale_color_brewer(palette="Set1")
library(wesanderson) ... + scale_color_manual(values=wes_palette(n=3, name="GrandBudapest"))
scale_color_grey(start=0.8, end=0.2) # start and end are optional
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)
scale_y_log10(labels = function(n){format(n, scientific = FALSE)})
scale_y_log10(labels = scales::comma) scale_y_log10(labels = function(n){format(n, scientific = FALSE, big.mark = ",")})
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))
- Themes
- Facet Labels
- Nested Facet Labels
- Removing Facet Labels
- Labels documentation:
- Label Bar Charts
- geomtext on facet dodged barplot
- geom_text
- geom_text
- r gallery - add text tables
- stackoverflow - specify geom text position by top/bottom/left/right
- Add target line to chart: How to add different lines for facets
- Excel: 3 Ways to Add a Target Line to an Excel Pivot Chart
- Bars on Log Plots
- geom_segment hack: direction of bars in ggplot barplot with log scale axes
- don't do it: Graph Makeover: Bars on a log scale
- Show all panes: Ctrl + Alt + Shift + 0