図の右上のshow
ボタンを押すとRのコードが表示されます。
library(conflicted)
library(tidyverse)
library(patchwork)
base_plot <- tibble(
x = seq(0, 10, length.out = 100),
y1 = sin(x),
y2 = cos(x),
y3 = y1 + y2
) |>
pivot_longer(!x, names_to = "y") |>
ggplot(aes(x = x, y = value, color = y, linetype = y)) +
coord_cartesian(xlim = c(0 ,10), ylim = c(-3, 3)) +
scale_color_hue(name = "", labels = c(y1 = "y = sin(x)", y2 ="y = cos(x)", y3 ="y = sin(x) + cos(x)")) +
scale_linetype_discrete(name = "", labels = c(y1 = "y = sin(x)", y2 ="y = cos(x)", y3 ="y = sin(x) + cos(x)")) +
theme(
legend.title = element_blank(),
legend.position = c(0.75, 0.85),
aspect.ratio = 1
)
# 見づらい図
p1 <- base_plot +
geom_line(linewidth = 0.2) +
scale_x_continuous(breaks = seq(0, 10, by = 0.5)) +
scale_y_continuous(breaks = seq(-3, 3, by = 0.2)) +
labs(x = "x-axis label, x", y = "y-axis label, y", title = "見づらいグラフ") +
theme(
axis.title = element_text(size = 6, family = "Times New Roman"),
axis.text = element_text(size = 6, family = "Times New Roman"),
legend.text = element_text(size = 8, family = "Times New Roman")
)
# 見やすい図
p2 <- base_plot +
geom_line(linewidth = 1) +
scale_x_continuous(breaks = seq(0, 10, by = 2)) +
scale_y_continuous(breaks = seq(-3, 3, by = 1)) +
labs(x = "x-axis label, x", y = "y-axis label, y", title = "見やすいグラフ") +
theme(
axis.title = element_text(size = 14, family = "Arial"),
axis.text = element_text(size = 14, family = "Arial"),
legend.text = element_text(size = 12, family = "Arial")
)
p1 + p2
library(conflicted)
library(tidyverse)
# データの定義
df <- data.frame(
x = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
y1 = c(11, 11.2, 11.5, 12, 12.2, 13, 14.2, 14.5, 15, 15.6, 16),
y2 = c(11.1, 11.3, 11.4, 11.9, 12.1, 12.8, 14.0, 14.2, 14.7, 15.0, 15.5)
) |>
pivot_longer(!x, names_to = "y")
# グラフ
p1 <- df |>
ggplot(aes(x = x, y = value, color = y, shape = y)) +
geom_line() +
geom_point() +
labs(x = "x", y = "y", title = "基準とする描画例") +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.8))
p2 <- df |>
ggplot(aes(x = x, y = value, color = y, shape = y)) +
geom_line() +
geom_point() +
labs(x = "x", y = "y", title = "縦軸範囲広げすぎ?") +
coord_cartesian(ylim = c(0, 30)) +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.8))
p3 <- df |>
ggplot(aes(x = x, y = value, color = y, shape = y)) +
geom_line() +
geom_point() +
labs(x = "x", y = "y", title = "縦軸方向を強調") +
theme(legend.title = element_blank(), legend.position = c(0.2, 0.9))
p4 <- df |>
ggplot(aes(x = x, y = value, color = y, shape = y)) +
geom_line() +
geom_point() +
labs(x = "x", y = "y", title = "横軸方向を強調") +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.8))
design <- "
113
223
444
"
p1 + p2 + p3 + p4 + plot_layout(design = design)
library(conflicted)
library(tidyverse)
library(zoo)
library(scales)
# 加工前のデータはこちらにあります。
# https://covid19.who.int/WHO-COVID-19-global-data.csv
# ファイルを読み込む
# 日本、アメリカ、中国のデータを抜き出す
# Date_reportedは日付型に
# 14日間の移動平均を計算
df <- read_csv("https://raw.githubusercontent.com/tkEzaki/data_visualization/main/8%E7%AB%A0/data/covid_data_dummy.csv") |>
dplyr::filter(Country %in% c("Japan", "United States of America", "China")) |>
mutate(
Date_reported = as.Date(Date_reported),
`14_day_avg` = rollmean(New_cases, 14, fill = NA),
.by = Country
) |>
mutate(Country = factor(Country, levels = c("Japan", "United States of America", "China")))
# 通常の縦軸
p1 <- df |>
ggplot(aes(x = Date_reported)) +
geom_line(aes(y = New_cases, color = Country)) +
scale_y_continuous(labels = label_comma()) +
theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
labs(x = "", y = "新規感染者数", title = "縦軸をそのままプロットしたもの") +
theme(legend.title = element_blank(), legend.position = c(0.2, 0.8), aspect.ratio = 1/2)
# 対数縦軸
p2 <- df |>
ggplot(aes(x = Date_reported)) +
geom_line(aes(y = New_cases, color = Country)) +
scale_y_continuous(trans = "log10", labels = label_comma()) +
theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
labs(x = "", y = "新規感染者数", title = "縦軸を対数でプロットしたもの") +
theme(legend.title = element_blank(), legend.position = "bottom", aspect.ratio = 1/2)
p1 / p2
コメントは「普段の平日より1.5倍の来客」となっているがコードは1.4倍?
原点がゼロになっていないが、ゼロにした
library(conflicted)
library(tidyverse)
library(ggrepel)
library(geomtextpath)
library(patchwork)
# データフレームを作成
# 月曜日から日曜日までの7日間, 各時刻
df_base <- expand_grid(
days_of_week = c("月", "火", "水", "木", "金", "土", "日"),
hours_of_day = 10:20
)
n <- nrow(df_base)
# 水曜日は12時と17時以外の時間に、普段の平日より1.5倍の来客
# 休日は11:00 - 17:00 までまんべんなく多い
# 平日は12時と17時が多い
set.seed(0)
df_visitor_count <- tibble(
days_of_week = factor(df_base$days_of_week, levels = c("月", "火", "水", "木", "金", "土", "日")),
hours_of_day = df_base$hours_of_day,
visitor_count = case_when(
days_of_week == "水" & !(hours_of_day %in% c(12, 17)) ~ sample(50:80, 77, replace = TRUE),
days_of_week == "水" & hours_of_day %in% c(12, 17) ~ sample(20:50, 77, replace = TRUE) * 1.5,
days_of_week %in% c("土", "日") & hours_of_day %in% 11:17 ~ sample(70:100, 77, replace = TRUE),
days_of_week %in% c("土", "日") & !(hours_of_day %in% 11:17) ~ sample(30:60, 77, replace = TRUE),
!(days_of_week %in% c("水", "土", "日")) & hours_of_day %in% c(12, 17) ~ sample(50:80, 77, replace = TRUE),
!(days_of_week %in% c("水", "土", "日")) & !(hours_of_day %in% c(12, 17)) ~ sample(20:50, 77, replace = TRUE)
)
)
# 折れ線グラフを描画
p1 <- df_visitor_count |>
ggplot(aes(x=hours_of_day, y=visitor_count, group=days_of_week, color=days_of_week)) +
scale_x_continuous(breaks = seq(10, 20, 1)) +
coord_cartesian(ylim = c(0, 100)) +
geom_line() +
geom_point() +
labs(x="時刻", y="来客数", color = "曜日", title = "凡例をまとめて表示した例") +
theme(aspect.ratio = 1/2)
# 凡例を近くに表示した例(1) ggrepelの例(個別に位置設定)
p2 <- df_visitor_count |>
ggplot(aes(x=hours_of_day, y=visitor_count, group=days_of_week, color=days_of_week)) +
scale_x_continuous(breaks = seq(10, 20, 1)) +
coord_cartesian(ylim = c(0, 100)) +
geom_line() +
geom_point() +
geom_text_repel(
data = data.frame(
days_of_week = c("月", "火", "水", "木", "金", "土", "日"),
hours_of_day = c( 14, 14, 20, 10.5, 14.1, 13, 16),
visitor_count = c( 37.5, 48, 79, 50, 21, 91, 96)
),
aes(label = days_of_week), size = 5
) +
theme(legend.position = "none", aspect.ratio = 1/2) +
labs(x="時刻", y="来客数", color = "曜日", title = "凡例を近くに表示した例(1)", subtitle = "ggrepel1")
# 凡例を近くに表示した例(2) ggrepelの例(終点に配置)
p3 <- df_visitor_count |>
ggplot(aes(x=hours_of_day, y=visitor_count, group=days_of_week, color=days_of_week)) +
scale_x_continuous(breaks = seq(10, 20, 1)) +
coord_cartesian(ylim = c(0, 100)) +
geom_line() +
geom_point() +
geom_text_repel(
data = df_visitor_count |> slice_max(hours_of_day, n = 1),
aes(label = days_of_week),
nudge_x = 1,
segment.alpha = 0.3,
size = 5
) +
theme(legend.position = "none", aspect.ratio = 1/2) +
labs(x="時刻", y="来客数", color = "曜日", title = "凡例を近くに表示した例(2)", subtitle = "ggrepel2")
# 凡例を近くに表示した例(2) geomtextpathの例 おしゃれだが日本語が通らない
p4 <- df_visitor_count |>
mutate(
days_of_week_en = case_when(
days_of_week == "月" ~ "Mon.",
days_of_week == "火" ~ "Tue.",
days_of_week == "水" ~ "Wed.",
days_of_week == "木" ~ "Thu.",
days_of_week == "金" ~ "Fri.",
days_of_week == "土" ~ "Sat.",
days_of_week == "日" ~ "Sun."
)
) |>
ggplot(aes(x=hours_of_day, y=visitor_count, group=days_of_week, color=days_of_week, label = days_of_week_en)) +
geom_textline(size = 4, vjust = -0.5) +
scale_x_continuous(breaks = seq(10, 20, 1)) +
coord_cartesian(ylim = c(0, 100)) +
geom_point() +
theme(legend.position = "none", aspect.ratio = 1/2) +
labs(x="時刻", y="来客数", color = "曜日", title = "凡例を近くに表示した例(3)", subtitle = "geomtextpath")
p1/p2/p3/p4
library(conflicted)
library(tidyverse)
# データフレームを作成
# 月曜日から日曜日までの7日間, 各時刻
df_base <- expand_grid(
days_of_week = c("月", "火", "水", "木", "金", "土", "日"),
hours_of_day = 10:20
)
n <- nrow(df_base)
# 水曜日は12時と17時以外の時間に、普段の平日より1.5倍の来客
# 休日は11:00 - 17:00 までまんべんなく多い
# 平日は12時と17時が多い
set.seed(0)
df_visitor_count <- tibble(
days_of_week = factor(df_base$days_of_week, levels = c("月", "火", "水", "木", "金", "土", "日")),
hours_of_day = df_base$hours_of_day,
visitor_count = case_when(
days_of_week == "水" & !(hours_of_day %in% c(12, 17)) ~ sample(50:80, 77, replace = TRUE),
days_of_week == "水" & hours_of_day %in% c(12, 17) ~ sample(20:50, 77, replace = TRUE) * 1.5,
days_of_week %in% c("土", "日") & hours_of_day %in% 11:17 ~ sample(70:100, 77, replace = TRUE),
days_of_week %in% c("土", "日") & !(hours_of_day %in% 11:17) ~ sample(30:60, 77, replace = TRUE),
!(days_of_week %in% c("水", "土", "日")) & hours_of_day %in% c(12, 17) ~ sample(50:80, 77, replace = TRUE),
!(days_of_week %in% c("水", "土", "日")) & !(hours_of_day %in% c(12, 17)) ~ sample(20:50, 77, replace = TRUE)
)
)
# 折れ線グラフを描画
days_of_week <- c("月", "火", "水", "木", "金", "土", "日")
df_visitor_count |>
ggplot(aes(x = hours_of_day, y = visitor_count, group = days_of_week, color = days_of_week, linetype = days_of_week)) +
geom_line() +
geom_point() +
scale_x_continuous(breaks = seq(10, 20, 1)) +
coord_cartesian(ylim = c(0, 100)) +
scale_color_manual(
values = c(rep("black", 2), "#F8766D", rep("black", 4)),
breaks = days_of_week
) +
scale_linetype_manual(
values = c(rep("solid", 5), rep("dashed", 2)),
breaks = days_of_week
) +
annotate("text", x = 13, y = 95, label="土日", size = 5, color = "black") +
annotate("text", x = 20, y = 85, label="水", size = 5, color = "#F8766D") +
annotate("text", x = 15, y = 15, label="その他の平日", size = 5, color = "black") +
labs(x = "時刻", y = "来客数", title = "注目しているデータだけハイライト") +
theme(legend.position = "none", aspect.ratio = 1/2)
library(conflicted)
library(tidyverse)
library(patchwork)
set.seed(5)
# 10変数、各時系列の長さはL=20でランダムデータを生成(正の値のみ)
# 乱数は0から1の範囲で生成されるため、それに50を(掛けて)足して負にならないようにする
df <- as.data.frame(
matrix(runif(20 * 6) * 50, ncol = 6),
make.names = FALSE
)
names(df) <- LETTERS[1:6]
# 相関係数を計算
correlation_matrix <- df |>
cor() |>
as.data.frame() |>
rownames_to_column()|>
pivot_longer(!rowname)
# データフレームを準備
df_plot <- df |>
pivot_longer(everything()) |>
mutate(name = factor(name, levels =LETTERS[6:1]))
# 棒グラフを描画
p1 <- df_plot |>
dplyr::filter(name == "A") |>
ggplot(aes(y = value, x=1:20, label = round(value, 2))) +
geom_col() +
coord_cartesian(xlim = c(1, 10)) +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
aspect.ratio = 1
)
# ヒートマップを描画
p2 <- correlation_matrix |>
ggplot(aes(x = rowname, y = name, fill = value, label = round(value, 2))) +
scale_fill_viridis_c(option = "turbo") +
geom_tile() +
theme_minimal() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
aspect.ratio = 1
)
# 数値入れる
p3 <- p1 +
geom_text(nudge_y = 1, size = 2.5)
p4 <- p2 +
geom_text(size = 3, aes(color = (value > 0.75 | value < -0.5))) +
scale_color_manual(values = c("black", "white")) +
guides(color = "none")
{p1 | p2} / {p3 | p4}
library(conflicted)
library(tidyverse)
library(patchwork)
data(iris)
base_p <- iris |>
ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point() +
theme(aspect.ratio = 1)
p1 <- base_p + labs(
x = expression(paste(italic("L")[italic("p")])),
y = expression(paste(italic("W")[italic("p")]))
) +
theme(legend.position = "none") +
labs(title = "軸ラベルがわかりにくい図")
p2 <- base_p +
labs(
x = expression(paste("花弁の長さ [cm], ", italic("L")[italic("p")])),
y = expression(paste("花弁の幅 [cm], ", italic("W")[italic("p")]))
) +
scale_color_hue(labels = c("setosa" = "セトサ", "versicolor" ="バージカラー", "virginica" ="バージニカ")) +
theme(
legend.position = c(0.2, 0.8),
legend.title = element_blank()
) +
labs(title = "軸ラベルをわかりやすくした図")
p1 + p2
library(conflicted)
library(tidyverse)
library(patchwork)
data(iris)
p1 <- iris |>
dplyr::filter(Species == "setosa") |>
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "花弁の幅 [cm]", title = "セトサ (花弁長 vs 花弁幅)") +
theme(aspect.ratio = 1)
p2 <- iris |>
dplyr::filter(Species == "setosa") |>
ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
labs(x = "がく片の長さ [cm]", y = "花弁の長さ [cm]", title = "セトサ (がく片長さ vs 花弁長)") +
theme(aspect.ratio = 1)
p3 <- iris |>
dplyr::filter(Species == "versicolor") |>
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "花弁の幅 [cm]", title = "バージカラー (花弁長 vs 花弁幅)") +
theme(aspect.ratio = 1)
p4 <- iris |>
dplyr::filter(Species == "versicolor") |>
ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
labs(x = "がく片の長さ [cm]", y = "花弁の長さ [cm]", title = "バージカラー (がく片長さ vs 花弁長)") +
theme(aspect.ratio = 1)
p5 <- iris |>
dplyr::filter(Species == "virginica") |>
ggplot(aes(x = Petal.Length, y = Petal.Width)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "花弁の幅 [cm]", title = "バージニカ (花弁長 vs 花弁幅)") +
theme(aspect.ratio = 1)
p6 <- iris |>
dplyr::filter(Species == "virginica") |>
ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
labs(x = "がく片の長さ [cm]", y = "花弁の長さ [cm]", title = "バージニカ (がく片長さ vs 花弁長)") +
theme(aspect.ratio = 1)
{p1 | p2 | p3} / {p4 | p5 | p6}
library(conflicted)
library(tidyverse)
library(patchwork)
data(iris)
plot_iris <- iris |>
mutate(
Species = case_when(
Species == "setosa" ~ "セトサ",
Species == "versicolor" ~ "バージカラー",
Species == "virginica" ~ "バージニカ"
)
)
p1 <- plot_iris |>
ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "花弁の幅 [cm]") +
theme(legend.position = "none", aspect.ratio = 1) +
facet_wrap(vars(Species), scales = "free")
p2 <- plot_iris |>
ggplot(aes(x = Petal.Length, y = Sepal.Length, color = Species)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "がく片の長さ [cm]") +
theme(legend.position = "none", aspect.ratio = 1) +
facet_wrap(vars(Species), scales = "free")
p1 / p2
これについては、スケールを揃えたほうが誤解が無いと思う。
こんな風に。
library(conflicted)
library(tidyverse)
library(patchwork)
data(iris)
plot_iris <- iris |>
mutate(
Species = case_when(
Species == "setosa" ~ "セトサ",
Species == "versicolor" ~ "バージカラー",
Species == "virginica" ~ "バージニカ"
)
)
p1 <- plot_iris |>
ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "花弁の幅 [cm]") +
theme(legend.position = "none", aspect.ratio = 1) +
facet_wrap(vars(Species))
p2 <- plot_iris |>
ggplot(aes(x = Petal.Length, y = Sepal.Length, color = Species)) +
geom_point() +
labs(x = "花弁の長さ [cm]", y = "がく片の長さ [cm]") +
theme(legend.position = "none", aspect.ratio = 1) +
facet_wrap(vars(Species))
p1 / p2
library(conflicted)
library(tidyverse)
library(ggbeeswarm)
n <- 20
set.seed(0)
samples <- purrr::map2(
c(10, 15, 12, 20), #mean
c(5, 5, 5, 5), #sd
\(x, y) rnorm(n, x, y)
)
# サンプルAに外れ値を追加
samples[[1]][1] <- 100
names(samples) <- LETTERS[1:4]
# データフレームを作成
df <- samples |>
as.data.frame() |>
pivot_longer(everything())
# スウォームプロット
p1 <- df |>
ggplot(aes(x = name, y = value, color = name)) +
geom_quasirandom() +
theme(
legend.position = "none",
axis.title = element_blank(),
aspect.ratio = 1
) +
labs(title = "元となるデータ")
# 棒グラフ(平均値)
p2 <- df |>
summarise(mean = mean(value), .by = name) |>
ggplot(aes(x = name, y = mean, fill = name)) +
geom_col() +
theme(
legend.position = "none",
axis.title = element_blank(),
aspect.ratio = 1
) +
labs(title = "指標化したもの(平均)")
p1 + p2
タイトルの斜体や下付き文字とオブジェクトが同居できない
教えていただいてできました!
library(conflicted)
library(tidyverse)
library(ggbeeswarm)
library(broom)
library(scales) #muted()
library(patchwork)
# データの生成
set.seed(2)
samples <- purrr::pmap(
list(
n = c(20, 20, 400, 400),
mean = c(6, 6, 6, 5.75),
sd = c(2, 2, 2, 2)
),
\(n, mean, sd) rnorm(n, mean, sd)
)
names(samples) <- c("v11", "v12", "v21", "v22")
# t検定
t_test1 <- tidy(t.test(samples$v11, samples$v12))
t_test2 <- tidy(t.test(samples$v21, samples$v22))
# データの整形
data1 <- data.frame(
Group = rep(c("Group 1", "Group 2"), each = 20),
Value = c(samples$v11, samples$v12)
)
data1_mean <- data1 |>
summarise(mean = mean(Value), .by = Group)
data2 <- data.frame(
Group = rep(c("Group 1", "Group 2"), each = 400),
Value = c(samples$v21, samples$v22)
)
data2_mean <- data2 |>
summarise(mean = mean(Value), .by = Group)
p1 <- ggplot() +
geom_col(data = data1_mean, aes(x = Group, y = mean, fill = Group )) +
geom_quasirandom(data = data1, aes(x = Group, y = Value, color = Group)) +
scale_fill_manual(values = c("#CAB2D6", "#B2DF8A")) +
scale_color_manual(values = c("#6A3D9A", "#33A02C")) +
coord_cartesian(ylim = c(0, 12)) +
labs(
title = "標本平均の差は大きいが……?",
subtitle = bquote(italic(t)[20] == .(round(t_test1$statistic, 2)) ~ ", p-value = " ~ .(round(t_test1$p.value, 4)))
# subtitle = expression(paste({italic(t)[20]}, " = ", round(t_test1$statistic, 2), ", p-value = ", round(t_test1$p.value, 4)))
# subtitle = paste("t20 = ", round(t_test1$statistic, 2), ", p-value = ", round(t_test1$p.value, 4))
) +
theme(legend.position = "none", axis.title = element_blank())
test <- expression({italic(t)[400]})
p2 <- ggplot() +
geom_col(data = data2_mean, aes(x = Group, y = mean, fill = Group )) +
geom_quasirandom(data = data2, aes(x = Group, y = Value, color = Group)) +
scale_fill_manual(values = c("#CAB2D6", "#B2DF8A")) +
scale_color_manual(values = c("#6A3D9A", "#33A02C")) +
coord_cartesian(ylim = c(0, 12)) +
labs(
title = "標本平均の差は小さいが……?",
subtitle = bquote(italic(t)[400] == .(round(t_test1$statistic, 2)) ~ ", p-value = " ~ .(round(t_test2$p.value, 4)))
# subtitle = paste("t400 = ", round(t_test2$statistic, 2), ", p-value = ", round(t_test2$p.value, 4))
) +
theme(legend.position = "none", axis.title = element_blank())
p1 + p2
第8章ここまで