knitr::opts_chunk$set( echo = F, message = F, warning = F, fig.path = "man/figures/", collapse = T, comment = "#>" ) library(jsmp)
This package does variety of things I find useful when working with R Tidyverse:
devtools::install_github("ymer/jsmp")
mtcars |> ggplot(aes(x = mpg, y = disp)) + geom_point()
gg_hist_percent
: Draws a histogram with y-axis as a percentage
gg_legend_remove
: Removes the legend
gg_legend_notitle
: Removes the legend title
gg_legend_bottom
: Moves the legend to the bottom of the plot
gg_loess_line
: Plots a locally weighed regression line
gg_regression_line
: Plots a regression line
gg_x_remove
: Removes the x-axis
gg_x_rotate
: Rotates x-axis labels 45°
gg_x_wrap
: Wraps text on x-axis labels
gg_y_big
: Changes the y-axis to avoid scientific notation for big numbers
gg_y_percent
: Changes the y-axis to percentage
gg_y_percent_zero
: Changes the y-axis to percentage, and adjusts the y-axis to start at 0.
gg_y_remove
: Removes the y-axis
gg_y_zero
: Adjusts the y-axis to start at exactly 0
summarise
: Overrides the standard summarise function, so that leftover grouping is dropped after summarisingmtcars |> group_by(am, gear) |> dplyr::summarise(mean_mpg = mean(mpg)) |> group_vars()
mtcars |> group_by(am, gear) |> summarise(mean_mpg = mean(mpg)) |> group_vars()
%notin%
: As %in%
but exclusionaryd
: Formats table (using gt
as base)ci_means
: Finds the means and confidence intervalsToothGrowth |> group_by(supp) |> ci_means(len) |> ggplot(aes(y = supp, x = mean)) + geom_crossbar(aes(xmin = ci.lower, xmax = ci.upper), width = 0.3, size = 0.7, color = c1)
ci_proportions
: Finds the proportions and confidence intervalsmtcars |> mutate(cyl = as.character(cyl)) |> count(cyl) |> mutate(total = sum(n)) |> ci_proportions() |> ggplot(aes(x = cyl, y = proportion)) + geom_col(position = position_dodge()) + gg_y_percent_zero() + gg_legend_notitle() + labs(y = "", x = "") + geom_errorbar(aes(ymin = ci.lower, ymax = ci.upper), width=.2, position=position_dodge(.9))
do_if
: Use a condition in a pipe flowonly_high_values <- T mtcars |> do_if(only_high_values, ~ . |> filter(disp > 180)) |> summarise(mean(disp))
only_high_values <- F mtcars |> do_if(only_high_values, ~ . |> filter(disp > 180)) |> summarise(mean(disp))
high_or_low <- "low" mtcars |> do_if(high_or_low == "high", ~ .x |> filter(disp >= 180), ~ .x |> filter(disp < 180)) |> summarise(mean(disp))
filter_duplicates
: selects duplicated rowsmtcars |> filter_duplicates(wt)
fix_names
: Changes the column names to tidy styleiris |> fix_names() |> head()
left_join0
: Performs a left_join, while setting values in missing rows to 0 instead of NA.df1 <- tribble( ~id, ~v1, 1, 2, 2, 2, 3, 10) df2 <- tribble( ~id, ~v2, 1, 2, 3, 4) left_join(df1, df2)
left_join0(df1, df2)
percent
: Returns proportion formatted as percentagepercent(0.173234235)
rows
: Facilitates loopingdf <- mtcars |> head() for (row in rows(df)){ print(row$mpg) }
tab
: Ordered count
including percentageToothGrowth |> filter(len > 20) |> tab(supp)
transpose
: Flips rows and columnsmtcars |> rownames_to_column() |> transpose()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.