Rのggplotでドラえもんを描いてみた

# こんなこといいな、できたらいいな
<!-- 発端や概要を記載 -->

こんなこといいな、できたらいいな
あんなゆめ こんなゆめ いっぱいあるけど〜🎶

Rでドラえもんをかきたいな

「はい！ggplot」


ということで, 本記事では, <code>R</code>を使った[ドラえもん](https://dora-world.com/)の描き方を紹介したいと思います。
22世紀のネコ型ロボット搭載され, データ分析が捗ること間違いなしです。

なお, 紹介するコードは, ChatGPTのコードを切り貼りしながら作成した「脳筋コード」ですので, 
もっと賢く描ける方法やひみつ道具があったら, ぜひぜひ教えてください〜



![DORAEMON.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2737734/9208c5d5-b1e3-9577-00ad-1b0bfd9be779.jpeg)




# 使う関数の定義します。


```r

#install.packages("tidyverse")
#install.packages("ggforce")
library(tidyverse)
library(ggforce)


# 円を描画する関数
draw_circle <- function(center, radius, color) {
  tibble(
    x = center[1],
    y = center[2],
    radius = radius,
    fill = color
  ) %>%
    ggplot(aes(x0 = x, y0 = y, r = radius)) +
    geom_circle(aes(fill = fill), color = "black") +
    scale_fill_identity() 
}


# 楕円を描画する関数
draw_ellipse <- function(center, radius_x, radius_y, color) {
  tibble(
    x = center[1],
    y = center[2],
    radius_x = radius_x,
    radius_y = radius_y,
    fill = color
  ) %>%
    ggplot(aes(x0 = x, y0 = y, a = radius_x, b = radius_y)) +
    geom_ellipse(aes(fill = fill), color = "black") 
}
```


# 描いてみるよ〜

```r
# 顔の輪郭
face <- draw_ellipse(c(0, -1), 9.7, 9, "skyblue")


# 顔の白い部分
cheek1 <- draw_ellipse(c(0, -2.5), 8.5, 7.5, "white")


# 目の輪郭
eye1 <- draw_ellipse(c(-2, 5), 2, 2.2, "white")
eye2 <- draw_ellipse(c(2, 5), 2, 2.2, "white")


# 目の黒目
pupil1 <- draw_ellipse(c(-1.2, 4.7), 0.6, 0.8, "black")
pupil2 <- draw_ellipse(c(1.2, 4.7), 0.6, 0.8, "black")


# 鼻の円
nose <- draw_circle(c(0, 2.6), 1, "red")
nose_line <- tibble(
  x = c(0, 0),
  y = c(3, -7),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 


# 口
x_vec <- seq(from = -7, to = 7, by = 0.01)
tmp_df <- tibble::tibble(
  x = x_vec, 
  y = -1 - sqrt(35 - x^2)
)


# 左のひげ
lMustache1 <- tibble(
  x = c(-3, -6.5),
  y = c(1, 2),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 
  
lMustache2 <- tibble(
  x = c(-3, -7),
  y = c(-0.5,-0.5),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 

lMustache3 <- tibble(
  x = c(-3, -6.5),
  y = c(-2, -3),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 


# 右のひげ
rMustache1 <- tibble(
  x = c(3, 6.5),
  y = c(1, 2),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 

rMustache2 <- tibble(
  x = c(3, 7),
  y = c(-0.5,-0.5),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 

rMustache3 <- tibble(
  x = c(3, 6.5),
  y = c(-2, -3),
  group = c(1, 1)
) %>%
  ggplot(aes(x, y, group = group)) 
  
   
# 各パーツを一つのプロットにまとめる
ggplot() +
  geom_ellipse(data = face$data, aes(x0 = x, y0 = y, a = radius_x, b = radius_y, angle=0), fill = face$data$fill, color = "black", size=3.5) +
  geom_ellipse(data = cheek1$data, aes(x0 = x, y0 = y, a = radius_x, b = radius_y, angle=0), fill = cheek1$data$fill, color = "black", size=3.5) +
  geom_ellipse(data = eye1$data, aes(x0 = x, y0 = y, a = radius_x, b = radius_y, angle=0), fill = eye1$data$fill, color = "black", size=3.5) +
  geom_ellipse(data = eye2$data, aes(x0 = x, y0 = y, a = radius_x, b = radius_y, angle=0), fill = eye2$data$fill, color = "black", size=3.5) +
  geom_ellipse(data = pupil1$data, aes(x0 = x, y0 = y, a = radius_x, b = radius_y, angle=0), fill = pupil1$data$fill, color = "black") +
  geom_ellipse(data = pupil2$data, aes(x0 = x, y0 = y, a = radius_x, b = radius_y, angle=0), fill = pupil2$data$fill, color = "black") +
  geom_line(data = nose_line$data, aes(x = x, y = y), color = "black", size=3.5) +
  geom_circle(data = nose$data, aes(x0 = x, y0 = y, r = radius), fill = nose$data$fill, color = "black", size=3.5) +
  geom_line(data = tmp_df, mapping = aes(x = x, y = y), color = "black", size=3.5) +
  geom_line(data = lMustache1$data, aes(x = x, y = y), color = "black", size=3.5) +
  geom_line(data = lMustache2$data, aes(x = x, y = y), color = "black", size=3.5) +
  geom_line(data = lMustache3$data, aes(x = x, y = y), color = "black", size=3.5) +
  geom_line(data = rMustache1$data, aes(x = x, y = y), color = "black", size=3.5) +
  geom_line(data = rMustache2$data, aes(x = x, y = y), color = "black", size=3.5) +
  geom_line(data = rMustache3$data, aes(x = x, y = y), color = "black", size=3.5) +
  scale_x_continuous(breaks = c(-10, 10), limits = c(-10, 10), expand = c( 0.1, 0.1 )) +
  scale_y_continuous(breaks = c(-10, 10), limits = c(-10, 10), expand = c( 0.1, 0.1 )) +
  theme_void()

ggsave(filename = "DORAEMON.jpeg",width = 800, height = 800, unit = "px", dpi = 100)
```

どどどどどどどどど ドラえもん！