令和4年 悩みやストレスの原因

Rで令和4年の国民生活基礎調査から年齢別の悩みやストレスの原因を面グラフを作るまで。 e-statのAPIを使って対象のデータを取得して、先頭の150行を表示。 options(crayon.enabled = FALSE) # 色無効化 library(tidyverse) library(httr) library(Polychrome) # palette36 __ ── 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.2 ✔ tibble 3.2.1 ✔ lubridate 1.9.4 ✔ tidyr 1.3.1 ✔ purrr 1.0.4 ── Conflicts ────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors apikey = "1234abcdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # e-statで取得した自分用のAPIキー # 国民生活基礎調査 令和4年国民生活基礎調査 健康 # 世帯人員(12歳以上),悩みやストレスの有-悩みやストレスの原因(複数回答)-無・性・年齢(5歳階級)別 # https://www.e-stat.go.jp/dbview?sid=0002040955 c <- paste0("http://api.e-stat.go.jp/rest/3.0/app/getSimpleStatsData?appId=", apikey, "&lang=J&statsDataId=0002040955&metaGetFlg=Y&cntGetFlg=N&explanationGetFlg=Y&annotationGetFlg=Y§ionHeaderFlg=1&replaceSpChars=0") res <- GET(c) text <- content(res, as = "text", encoding = "UTF-8") lines <- strsplit(text, "\n")[[1]] head(lines, 150) ...

作成日: 2025年6月15日

【ラクにしたいでR】投稿記事のアイキャッチ画像作成

Rでパッケージ{magick}を使って投稿記事のアイキャッチ画像(featured image)を作る話。 tidyverseに出会ってRにはまっている。 パイプで処理をつなげていけるって分かりやすい。 magickも同様にパイプで処理ができる。 まずmagickパッケージを読み込む。 library(magick) ## Linking to ImageMagick 6.9.11.60 ## Enabled features: fontconfig, freetype, fftw, heic, lcms, pango, webp, x11 ## Disabled features: cairo, ghostscript, raw, rsvg ## Using 4 threads 以下のコードでpng画像が作成できる。 # 初期設定 filename <- "Reyecatch.png" title <- "投稿記事のアイキャッチ画像作成" fontsize <- 6 # 画像作成 rlogo <- image_read("Rlogo.png") |> image_resize("x200") img1 <- image_blank(width = 1200, height = 628, color = 'white') img1 <- image_draw(img1) rect(0, 0, 1200, 314, col = "white", border = NA) rect(0, 314, 1200, 628, col = "#2369bd", border = NA) text(740, 157, "ラクにしたいでR", family = "Noto Sans Mono CJK JP", col = 'black', cex = 8) text(600, 157+314, title, family = "Noto Sans Mono CJK JP", col = 'white', cex = fontsize) img1 <- img1 |> image_border(color = "#2369bd", geometry = "10x10") |> image_composite(rlogo, offset = "+65+65") image_write(img1, filename) # 確認用 img1 |> image_resize("400x") |> print() ## format width height colorspace matte filesize density ## 1 png 400 212 sRGB TRUE 0 72x72 文字数によってフォントサイズを調整する必要があるのがちょっと手間だけど、微調整はきっと必要なのでいったんはこれでヨシ! ...

作成日: 2024年12月21日

【ラクにしたいでR】ggplotの棒グラフで件数を描画する

「はい」、「いいえ」で答えた件数をggplotで棒グラフに描画する方法を紹介する。 データの準備 AさんからHさんまでの8名がある質問にYesかNoで答えたとする。 person = c("A", "B", "C", "D", "E", "F", "G", "H") answer = c("Y", "Y", "Y", "N", "Y", "Y", "N", "Y") df <- data.frame(person, answer) print(df) ## person answer ## 1 A Y ## 2 B Y ## 3 C Y ## 4 D N ## 5 E Y ## 6 F Y ## 7 G N ## 8 H Y グラフ描画 ggplotなどを含むtidyverseを読み込む。 library(tidyverse) まずはシンプルに棒グラフ化したもの。 df |> ggplot(aes(answer))+ geom_bar() 件数を描画する。 df |> ggplot(aes(answer))+ geom_bar() + geom_text(stat = "count", aes(label = after_stat(count))) 色分けしたもの。 df |> ggplot(aes(answer))+ geom_bar(aes(fill = answer)) + geom_text(stat = "count", aes(label = after_stat(count))) 位置や大きさを調整したもの。 df |> ggplot(aes(answer))+ geom_bar(aes(fill = answer)) + geom_text(stat = "count", aes(label = after_stat(count)), nudge_y = -1, size = 10) ...

作成日: 2024年12月15日