Skip to content

Instantly share code, notes, and snippets.

View jonocarroll's full-sized avatar
👨‍💻
Learning all the languages

Jonathan Carroll jonocarroll

👨‍💻
Learning all the languages
View GitHub Profile
## load the data.table package
library(data.table)
## make this reproducible (up to memory locations)
set.seed(123)
## create a data.table with some dummy data
DT1 <- data.table(x=1:5, y=runif(5))
DT1
# x y
## load the usual packages
library(magrittr)
library(dplyr)
library(ggplot2)
## install and load the likert package from GitHub
# devtools::install_github('jbryer/likert')
library(likert)
## load the data downloaded from the survey
@jonocarroll
jonocarroll / stack.R
Created May 27, 2016 06:43
How did I not know about stack(), unstack(), and applying formula to a data.frame?
tmp <- data.frame(a = c(1, 2, 3), b = c(4, 3, 5), c = c(4, 4, 5))
tmp
# a b c
# 1 1 4 4
# 2 2 3 4
# 3 3 5 5
formula(tmp)
# a ~ b + c
@jonocarroll
jonocarroll / add_images_as_xlabels.R
Created June 2, 2016 12:10
Replace categorical x-axis labels with images
#' Replace categorical x-axis labels with images
#'
#' Pipe a ggplot2 graph (with categorical x-axis) into this function with the argument of a list of
#' pictures (e.g. loaded via readImage) and it builds a new grob with the x-axis categories
#' now labelled by the images. Solves a problem that you perhaps shouldn't have.
#'
#' @author J. Carroll, \email{jono@@jcarroll.com.au}
#' @references \url{http://stackoverflow.com/questions/29939447/icons-as-x-axis-labels-in-r-ggplot2}
#'
#' @param g ggplot graph with categorical x axis
@jonocarroll
jonocarroll / GDP_per_capita.R
Created June 2, 2016 12:11
GDP per capita. Used as an example for add_images_as_xlabels which does what it suggests.
library(rvest)
## GDP per capita, top 10 countries
url <- "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita"
html <- read_html(url)
gdppc <- html_table(html_nodes(html, "table")[3])[[1]][1:10,]
## clean up; remove non-ASCII and perform type conversions
gdppc$Country <- gsub("Â ", "", gdppc$Country)
gdppc$Rank <- iconv(gdppc$Rank, "latin1", "ASCII", sub="")
@jonocarroll
jonocarroll / images_as_xaxis_labels_updated.R
Last active September 7, 2021 05:43
Implements @baptiste's much better method for making this work
library(ggplot2) ## devtools::install_github("hadley/ggplot2)
library(grid) ## rasterGrob
library(EBImage) ## readImage (alternatively: magick::image_read)
library(ggthemes) ## theme_minimal
## ##########
## INDEPENDENT CODE TO BE SOURCED:
## ##########
# user-level interface to the element grob
## Based on BUILDING A GGPLOT2 STEP BY STEP
## https://matthewdharris.com/2016/08/12/ggplot2-step-by-step/
## 'reproducible' saving of data:
## this was to be copied by hand, hoping that Unicode chars didn't get caught up
## requires re-formatting to factors, etc...
year <- c(1760, 1790, 1797, 1850, 1860, 1889, 1900, 1910, 1950)
sites <- c("Isleta", "Acoma", "Laguna", "Zuni", "Sandia", "San Felipe",
"Santa Ana", "Zia", "Santo Domingo", "Jemez", "Cochiti",
"Tesuque", "Nambe", "San Ildefonso", "Pojoaque", "Santa Clara",
@jonocarroll
jonocarroll / RDocTaskForceLogo.md
Last active September 22, 2016 05:41
Logo suggestions for R Documentation Task Force

This is merely a suggestion.

  • the R SVG logo with the function code for help() overlayed (not all of it).
@jonocarroll
jonocarroll / so-answers.R
Created October 21, 2016 04:19
Analysis of my own Stack Overflow Answers via the Kaggle dataset
## Process the Stack Overflow Answers data for my own account
## jonathan-carroll -- 4168169
##
## Jonathan Carroll
## 21 October, 2016
##
## https://twitter.com/carroll_jono/status/789319774773714946
## load required packages
library(dplyr)
@jonocarroll
jonocarroll / analyse_packages.R
Created December 15, 2016 01:14
Analyse an R script and determine which package the functions are likely to be from
library(dplyr)
library(ggplot2)
find_functions <- function(file) {
getParseData(parse(file = file)) %>%
filter(token == "SYMBOL_FUNCTION_CALL") %>%
distinct(text) %>%
use_series(text)
}