options(digits = 2)
set.seed(2)
d0 <- data.frame(
A = c(8,12,16,7,3,11,7,4,10,13,15,6,18,5),
B = c(15,17,18,21,20,9,22,14,15,6,5,23,13,17))
d <- round(d0, 2)
str(d)
## 'data.frame': 14 obs. of 2 variables:
## $ A: num 8 12 16 7 3 11 7 4 10 13 ...
## $ B: num 15 17 18 21 20 9 22 14 15 6 ...
summary(d)
## A B
## Min. : 3.0 Min. : 5.0
## 1st Qu.: 6.2 1st Qu.:13.2
## Median : 9.0 Median :16.0
## Mean : 9.6 Mean :15.4
## 3rd Qu.:12.8 3rd Qu.:19.5
## Max. :18.0 Max. :23.0
library(DT)
datatable(d, caption = '数', editable = 'cell', rownames = F)
COL <- c(rgb(255, 0, 0, 105, max = 255),
rgb( 0, 0, 255, 105, max = 255))
boxplot(d, col = COL,
ylim = c(0, 30),
names = c('A', 'B'),
main = '箱ひげ図',
xlab = '',
ylab = '数[個]')
abline(h = seq(-50, 50, 5), lty = 2, col = gray(.5, .25))
library(plotly)
## 要求されたパッケージ ggplot2 をロード中です
##
## 次のパッケージを付け加えます: 'plotly'
## 以下のオブジェクトは 'package:ggplot2' からマスクされています:
##
## last_plot
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter
## 以下のオブジェクトは 'package:graphics' からマスクされています:
##
## layout

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ds <- pivot_longer(d, cols = c('A', 'B'))
kyokasho <- list(size = 11,
color = 'blue',
family = 'UD Digi Kyokasho NK-R')
plot_ly(x = ds$name, y = ds$value, split = ds$name, type = 'box',
boxpoints = 'all', jitter = 0.3, pointpos = -1.8) |>
layout(font = kyokasho,
title = '箱ひげ図',
xaxis = list(title = ''),
yaxis = list(title = '数[個]'))
library(vioplot)
## 要求されたパッケージ sm をロード中です
## Package 'sm', version 2.2-6.0: type help(sm) for summary information
## 要求されたパッケージ zoo をロード中です
##
## 次のパッケージを付け加えます: 'zoo'
##
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## as.Date, as.Date.numeric
vioplot(d, col = COL, rectCol = 'white',
plotCentre = 'line',
ylim = c(-0, 30),
names = c('A', 'B'),
main = 'バイオリン図',
xlab = '',
ylab = '数[個]')
points(apply(d, 2, mean), pch = 3)
abline(h = seq(-50, 50, 5), lty = 2, col = gray(0.5, 0.25))
legend('topleft', pch = 3, legend = '平均値')

plot_ly(x = ds$name, y = ds$value, split = ds$name, type = 'violin',
box = list(visible = T), meanline = list(visible = T)) |>
layout(font = kyokasho,
title = 'バイオリン図',
xaxis = list(title = ''),
yaxis = list(title = '数[個]'))