Skip to content

Instantly share code, notes, and snippets.

View fnshr's full-sized avatar

Fumiaki Nishihara (西原史暁) fnshr

View GitHub Profile
@fnshr
fnshr / iris-scatterplot.R
Last active July 20, 2018 12:13
Create the scatterplot in the High School Course of Study Guide for Mathematics of Japan (2018)
# The current stable version of {car} package (Ver. 3.0-0) does not output scatterplots properly.
# Instead, use the development version as following:
# install.packages("car", repos="http://R-Forge.R-project.org")
car::scatterplot(Sepal.Length ~ Petal.Width | Species,
data = iris,
boxplots= "xy", xlab="", ylab="",
grid = FALSE, legend = FALSE,
col = "black",
regLine = FALSE, smooth = FALSE)
@fnshr
fnshr / hex_sf.R
Last active July 22, 2018 00:47
Create a hexagonal map as an sf object
if(!require("sf")){
install.packages("sf")
library("sf")
}
hex_sf <- function(x, y, radius = 1){
stopifnot(length(y) == length(x) &&
is.integer(x) && is.integer(y))
center_x <- sqrt(3) * radius * x +
sqrt(3) / 2 * radius * y
@fnshr
fnshr / geom_simpler_hex.R
Last active July 22, 2018 01:01
geom_ function for ggplot2 to create a hexagonal map
hexGrob <- function(x, y, size = rep(1, length(x)), gp = gpar()) {
stopifnot(length(y) == length(x))
n <- length(x)
dx <- resolution(x, FALSE)
dy <- resolution(y, FALSE)
if(dx == 1) dx <- dy
if(dy == 1) dy <- dx
dratio <- dy/dx
grid::polygonGrob(rep.int(cos(pi/2 + pi/3 * 0:5)*dx/sqrt(3), n) +
rep(x + y/2/dratio, each = 6),
@fnshr
fnshr / find-prime-dates.R
Created December 31, 2020 11:51
Find new year's days whose dates are prime numbers and whose previous dates are also prime numbers.
library("numbers") # to use "isPrime" function
# Prepare dates
year <- 1000:2999
nyeve <- as.integer(paste0(year, "1231"))
ny <- as.integer(paste0(year + 1, "0101"))
# Output
bool_successive <- isPrime(nyeve) & isPrime(ny)
cat(paste(nyeve[bool_successive], ny[bool_successive], sep = ", "), sep = "\n")
library("tidyverse")
library("stringr")
library("leaflet")
library("leaflet.extras")
library("sf")
# 日本地図のデータ
# https://oku.edu.mie-u.ac.jp/~okumura/stat/shape.html
japanmap <- read_sf("https://okumuralab.org/~okumura/stat/data/japan.geojson")