Image by Allison Horst
Rには備え付けの関数、plot()があるが、今回はtidyverseのggplot2パッケージを使う
gg = “Grammar of Graphics”(画像の文法)
図の構造にはいくつか決まった要素がある
geometry: 図はどのような形にする?
aesthetics: データをどのように図に表す?
Pie chart

Bar graph


palmerpenguinsパッケージに入っている
三種類のペンギンのデータ(体重、くちばしや翼の大きさなど)を含む
# A tibble: 344 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm
<fct> <fct> <dbl> <dbl> <int>
1 Adelie Torgersen 39.1 18.7 181
2 Adelie Torgersen 39.5 17.4 186
3 Adelie Torgersen 40.3 18 195
4 Adelie Torgersen NA NA NA
5 Adelie Torgersen 36.7 19.3 193
6 Adelie Torgersen 39.3 20.6 190
7 Adelie Torgersen 38.9 17.8 181
8 Adelie Torgersen 39.2 19.6 195
9 Adelie Torgersen 34.1 18.1 193
10 Adelie Torgersen 42 20.2 190
# ℹ 334 more rows
# ℹ 3 more variables: body_mass_g <int>, sex <fct>, year <int>
Rows: 344
Columns: 8
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Ad…
$ island <fct> Torgersen, Torgersen, Torgersen, Torgersen…
$ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39…
$ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19…
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193…
$ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 46…
$ sex <fct> male, female, female, NA, female, male, fe…
$ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, …
ggplot()でグラフの基盤を作るmapping()で座標を指定するmapping()で座標を指定するgeom_()でデータの形を指定するgeom_()でデータの形を指定するbill_length_mmを横軸、bill_depth_mmを縦軸にして、点グラフを作成して下さい
colorで色を潰すcolorで色を潰すgeom_smooth()回帰直線を付け加えるgeom_smooth()回帰直線を付け加える

aes設定はgeom_()の中でもできるgeom_smooth()とgeom_point()で指定したい要素を考えよう

colorをgeom_point()だけ指定しようshapeで点の形を変えるaesの中でshapeを使うことによって、それぞれの種を点の形で表しましょう
labs()でラベルをきれいにするggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(mapping = aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)", y = "Body mass (g)",
color = "Species", shape = "Species"
)
labs()でラベルを整えるggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(mapping = aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)", y = "Body mass (g)",
color = "Species", shape = "Species"
)
scale_colorで色を変えるggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(mapping = aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)", y = "Body mass (g)",
color = "Species", shape = "Species"
) +
scale_color_colorblind()
scale_colorで色を変えるggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(mapping = aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)", y = "Body mass (g)",
color = "Species", shape = "Species"
) +
scale_color_colorblind()
data =とmapping =を書かなくて良い
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)", y = "Body mass (g)",
color = "Species", shape = "Species"
) +
scale_color_colorblind()
base_familyで文字化けを防ぐggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "体重とフリッパーの長さ",
subtitle = "アデリー、チンストラップ、ジェンツーのペンギンのサイズ",
x = "フリッパーの長さ(mm)", y = "体重(g)",
color = "種", shape = "種"
) +
scale_color_colorblind() +
theme_gray(base_family = "HiraKakuPro-W3")
base_familyで文字化けを防ぐggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
labs(
title = "体重とフリッパーの長さ",
subtitle = "アデリー、チンストラップ、ジェンツーのペンギンのサイズ",
x = "フリッパーの長さ(mm)", y = "体重(g)",
color = "種", shape = "種"
) +
scale_color_colorblind() +
theme_gray(base_family = "HiraKakuPro-W3")
mappingとaes)、どのデータをグラフのどの要素で表示するかを設定するgeom)は、グラフの形状を設定する+で重ねていくレポートでは、自分で選んだデータを使って、解析と可視化を行ってください。
もし自分のデータがない場合は、TidyTuesdayのデータを使ってもかまいません。
締め切り:第7回(5月27日)
library(tidytuesdayR)
tuesdata <- tidytuesdayR::tt_load(2025, week = 13)
pokemon_df <- tuesdata$pokemon_df
pokemon_df# A tibble: 949 × 22
id pokemon species_id height weight base_experience type_1
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
1 1 bulbasaur 1 0.7 6.9 64 grass
2 2 ivysaur 2 1 13 142 grass
3 3 venusaur 3 2 100 236 grass
4 4 charmander 4 0.6 8.5 62 fire
5 5 charmeleon 5 1.1 19 142 fire
6 6 charizard 6 1.7 90.5 240 fire
7 7 squirtle 7 0.5 9 63 water
8 8 wartortle 8 1 22.5 142 water
9 9 blastoise 9 1.6 85.5 239 water
10 10 caterpie 10 0.3 2.9 39 bug
# ℹ 939 more rows
# ℹ 15 more variables: type_2 <chr>, hp <dbl>, attack <dbl>,
# defense <dbl>, special_attack <dbl>, special_defense <dbl>,
# speed <dbl>, color_1 <chr>, color_2 <chr>, color_f <chr>,
# egg_group_1 <chr>, egg_group_2 <chr>, url_icon <chr>,
# generation_id <dbl>, url_image <chr>