0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ggplot2のサンプルデータセット

Posted at

ggplot2には、ダイヤモンドのデータセットなどのサンプルデータセットがあります。
今回は、ggplot2に付属しているデータセットを紹介します。
実行環境:MacBook Air M1, 2020

RとRStudioのインストール

以下のURLから、RとRStudioをインストールします。
https://posit.co/download/rstudio-desktop/
インストール方法は以下のURLの記事にわかりやすく書かれています。
https://qiita.com/azzeten/items/1031c788ed093d3b3946

ggplot2のインストールと読み込み

ggplot2のインストールと読み込みを行います。

RStudioを起動し、以下のコードを実行します。

install.packages("tidyverse")
library(ggplot2)

次回からは、以下のコードのみを実行します。

library(ggplot2)

これにより、ggplot2を読み込むことができます。

diamonds

diamondsは、53940個のダイヤモンドの価格や他の属性を含むデータセットです。

data = as.data.frame(diamonds)
head(data)
  carat       cut color clarity depth table price    x    y    z
1  0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2  0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
3  0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
4  0.29   Premium     I     VS2  62.4    58   334 4.20 4.23 2.63
5  0.31      Good     J     SI2  63.3    58   335 4.34 4.35 2.75
6  0.24 Very Good     J    VVS2  62.8    57   336 3.94 3.96 2.48

economics、economics_long

economicsはアメリカの経済時系列データから作成されたデータセットです。
economicsはワイドフォーマット、economics_longはロングフォーマットです。

economics

data = as.data.frame(economics)
head(data)
        date   pce    pop psavert uempmed unemploy
1 1967-07-01 506.7 198712    12.6     4.5     2944
2 1967-08-01 509.8 198911    12.6     4.7     2945
3 1967-09-01 515.6 199113    11.9     4.6     2958
4 1967-10-01 512.2 199311    12.9     4.9     3143
5 1967-11-01 517.4 199498    12.8     4.7     3066
6 1967-12-01 525.1 199657    11.8     4.8     3018

economics_long

data = as.data.frame(economics_long)
head(data)
        date variable value      value01
1 1967-07-01      pce 506.7 0.0000000000
2 1967-08-01      pce 509.8 0.0002652497
3 1967-09-01      pce 515.6 0.0007615234
4 1967-10-01      pce 512.2 0.0004706043
5 1967-11-01      pce 517.4 0.0009155394
6 1967-12-01      pce 525.1 0.0015743854

faithfuld

faithfuldはアメリカのワイオミング州のイエローストーン国立公園にあるオールド・フェイスフル・ガイザーの噴火データの2D密度推定です。

data = as.data.frame(faithfuld)
head(data)
  eruptions waiting     density
1  1.600000      43 0.003216159
2  1.647297      43 0.003835375
3  1.694595      43 0.004435548
4  1.741892      43 0.004977614
5  1.789189      43 0.005424238
6  1.836486      43 0.005744544

midwest

midwestは2000年のアメリカ国勢調査から作成された中西部の人口統計情報です。

data = as.data.frame(midwest)
head(data)

出力は28列あるため省略します。

mpg

mpgは38の人気車種の1999年と2008年の燃費のデータセットです。

data = as.data.frame(mpg)
head(data)
  manufacturer model displ year cyl      trans drv cty hwy fl   class
1         audi    a4   1.8 1999   4   auto(l5)   f  18  29  p compact
2         audi    a4   1.8 1999   4 manual(m5)   f  21  29  p compact
3         audi    a4   2.0 2008   4 manual(m6)   f  20  31  p compact
4         audi    a4   2.0 2008   4   auto(av)   f  21  30  p compact
5         audi    a4   2.8 1999   6   auto(l5)   f  16  26  p compact
6         audi    a4   2.8 1999   6 manual(m5)   f  18  26  p compact

msleep

msleepは哺乳類の睡眠のデータセットです。

data = as.data.frame(msleep)
head(data)
                        name      genus  vore        order conservation sleep_total sleep_rem sleep_cycle awake brainwt  bodywt
1                    Cheetah   Acinonyx carni    Carnivora           lc        12.1        NA          NA  11.9      NA  50.000
2                 Owl monkey      Aotus  omni     Primates         <NA>        17.0       1.8          NA   7.0 0.01550   0.480
3            Mountain beaver Aplodontia herbi     Rodentia           nt        14.4       2.4          NA   9.6      NA   1.350
4 Greater short-tailed shrew    Blarina  omni Soricomorpha           lc        14.9       2.3   0.1333333   9.1 0.00029   0.019
5                        Cow        Bos herbi Artiodactyla domesticated         4.0       0.7   0.6666667  20.0 0.42300 600.000
6           Three-toed sloth   Bradypus herbi       Pilosa         <NA>        14.4       2.2   0.7666667   9.6      NA   3.850

presidential

presidentialはアイゼンハワーからトランプまでの12人のアメリカ大統領の名前、任期の開始日と終了日、政党のデータセットです。

data = as.data.frame(presidential)
head(data)
        name      start        end      party
1 Eisenhower 1953-01-20 1961-01-20 Republican
2    Kennedy 1961-01-20 1963-11-22 Democratic
3    Johnson 1963-11-22 1969-01-20 Democratic
4      Nixon 1969-01-20 1974-08-09 Republican
5       Ford 1974-08-09 1977-01-20 Republican
6     Carter 1977-01-20 1981-01-20 Democratic

seals

sealsはアザラシの動きのベクトル場のデータセットです。

data = as.data.frame(seals)
head(data)
   lat   long delta_long  delta_lat
1 29.7 -172.8 -0.9150462 0.14347525
2 30.7 -172.8 -0.8670125 0.12838872
3 31.7 -172.8 -0.8189249 0.11323248
4 32.7 -172.8 -0.7707763 0.09802037
5 33.7 -172.8 -0.7225597 0.08276624
6 34.7 -172.8 -0.6742680 0.06748393

txhousing

txhousingはテキサス州の住宅市場のデータセットです。

data = as.data.frame(txhousing)
head(data)
     city year month sales   volume median listings inventory     date
1 Abilene 2000     1    72  5380000  71400      701       6.3 2000.000
2 Abilene 2000     2    98  6505000  58700      746       6.6 2000.083
3 Abilene 2000     3   130  9285000  58100      784       6.8 2000.167
4 Abilene 2000     4    98  9730000  68600      785       6.9 2000.250
5 Abilene 2000     5   141 10590000  67300      794       6.8 2000.333
6 Abilene 2000     6   156 13910000  66900      780       6.6 2000.417

luv_colours

luv_coloursは色をLuv色空間に変換したデータセットです。

data = as.data.frame(luv_colours)
head(data)
         L             u         v           col
1 9341.570 -3.370649e-12    0.0000         white
2 9100.962 -4.749170e+02 -635.3502     aliceblue
3 8809.518  1.008865e+03 1668.0042  antiquewhite
4 8935.225  1.065698e+03 1674.5948 antiquewhite1
5 8452.499  1.014911e+03 1609.5923 antiquewhite2
6 7498.378  9.029892e+02 1401.7026 antiquewhite3

ggplot2には、様々なデータセットが付属していることがわかりました。
データ分析を勉強する方のサンプルデータセットとしておすすめです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?