d <- data.frame(a = c(5, 8, 4, 9),
b = c(1, 2, 3, 4),
c = c(9, 8, 7, 5))
rownames(d) <- c("2022年5月",
"2022年6月",
"2022年7月",
"2022年8月")
colnames(d) <- c("チワワ",
"トイプードル",
"豆柴")
library(kableExtra)
kable(d, caption = "販売数[匹]")
販売数[匹]
2022年5月 |
5 |
1 |
9 |
2022年6月 |
8 |
2 |
8 |
2022年7月 |
4 |
3 |
7 |
2022年8月 |
9 |
4 |
5 |
#積上棒グラフ
# Load necessary libraries
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
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::group_rows() masks kableExtra::group_rows()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(kableExtra)
# Create the data frame
d <- data.frame(a = c(5, 8, 4, 9),
b = c(1, 2, 3, 4),
c = c(9, 8, 7, 5))
# Assign row names
rownames(d) <- c("2022年5月",
"2022年6月",
"2022年7月",
"2022年8月")
# Assign column names
colnames(d) <- c("チワワ",
"トイプードル",
"豆柴")
# Convert row names to a column
d <- d %>%
rownames_to_column(var = "Month")
# Pivot data from wide to long format
d_long <- d %>%
pivot_longer(cols = -Month,
names_to = "Breed",
values_to = "Count")
# Plot the stacked bar chart
ggplot(d_long, aes(x = Month, y = Count, fill = Breed)) +
geom_bar(stat = "identity") +
labs(title = "販売数[匹]",
x = "月",
y = "数") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

#時系列グラフ
# Load necessary libraries
library(tidyverse)
library(kableExtra)
# Create the data frame
d <- data.frame(a = c(5, 8, 4, 9),
b = c(1, 2, 3, 4),
c = c(9, 8, 7, 5))
# Assign row names
rownames(d) <- c("2022年5月",
"2022年6月",
"2022年7月",
"2022年8月")
# Assign column names
colnames(d) <- c("チワワ",
"トイプードル",
"豆柴")
# Convert row names to a column
d <- d %>%
rownames_to_column(var = "Month")
# Pivot data from wide to long format
d_long <- d %>%
pivot_longer(cols = -Month,
names_to = "Breed",
values_to = "Count")
# Plot the time series graph
ggplot(d_long, aes(x = Month, y = Count, color = Breed, group = Breed)) +
geom_line() +
geom_point() +
labs(title = "販売数[匹]",
x = "月",
y = "数") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

#相関図
# Load necessary libraries
library(tidyverse)
library(kableExtra)
# Create the data frame
d <- data.frame(a = c(5, 8, 4, 9),
b = c(1, 2, 3, 4),
c = c(9, 8, 7, 5))
# Assign row names
rownames(d) <- c("2022年5月",
"2022年6月",
"2022年7月",
"2022年8月")
# Assign column names
colnames(d) <- c("チワワ",
"トイプードル",
"豆柴")
# Convert row names to a column
d <- d %>%
rownames_to_column(var = "Month")
# Plot the scatter plot for correlation between トイプードル and 豆柴
ggplot(d, aes(x = `トイプードル`, y = `豆柴`)) +
geom_point(size = 3) +
geom_smooth(method = "lm", col = "blue") +
labs(title = "トイプードルと豆柴の販売数の相関図",
x = "トイプードルの販売数",
y = "豆柴の販売数") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#意味のある相関係数
d <- data.frame(
トイプードル = c(1, 2, 3, 4),
豆柴= c(9, 8, 7, 5))
library(DT)
datatable(d)
#psych パッケージを利用したグラフ
library(psych)
##
## 次のパッケージを付け加えます: 'psych'
## 以下のオブジェクトは 'package:ggplot2' からマスクされています:
##
## %+%, alpha
pairs.panels(d)

cor.plot(d)
## Warning in abbreviate(rownames(r), minlength = minlength): ASCII
## 文字でないものが省略名として使われました
## Warning in abbreviate(colnames(r), minlength = minlength): ASCII
## 文字でないものが省略名として使われました
## Warning in abbreviate(dimnames(ans)[[2L]], minlength = abbr.colnames): ASCII
## 文字でないものが省略名として使われました

#インタラクティブグラフ
library(plotly)
##
## 次のパッケージを付け加えます: 'plotly'
## 以下のオブジェクトは 'package:ggplot2' からマスクされています:
##
## last_plot
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter
## 以下のオブジェクトは 'package:graphics' からマスクされています:
##
## layout
# フォント設定
kyokasho <- list(size = 11, color = 'blue', family = 'UD Digi Kyokasho NK-R')
plot_ly(x = rownames(d),
y = colnames(d),
z = as.matrix(d),
text = paste(d),
type = 'heatmap') |>
layout(font = kyokasho,
title = '販売数{匹}',
xaxis = list(title = '種類'),
yaxis = list(title = '匹数'))
library(tidyverse)
url <- "https://stats.dip.jp/01_ds/data/rate_manga_read.csv"
data <- read.csv(url)
str(data)
## 'data.frame': 47 obs. of 3 variables:
## $ 都道府県: chr "北海道" "青森県" "岩手県" "宮城県" ...
## $ 男 : num 39.1 32.7 33.7 41.5 33.2 36.6 38.4 37.7 35.3 38.5 ...
## $ 女 : num 32.4 26.3 29.1 36.4 27 29 30.3 31.6 30.6 31.2 ...
head(data)
## 都道府県 男 女
## 1 北海道 39.1 32.4
## 2 青森県 32.7 26.3
## 3 岩手県 33.7 29.1
## 4 宮城県 41.5 36.4
## 5 秋田県 33.2 27.0
## 6 山形県 36.6 29.0