5

絵文字を含む Unicode テキストがあります。絵文字の色を含む方法で geom_text または geom_label を使用して ggplot2 グラフィックでそれらをレンダリングしたいと思います。私は見てきましたemojifontemoそしてggtext、これらのどれもこれを許可していないようです。もちろん問題は、テキストの色がgeom_text色の美学に左右されることです。geom_text またはその他の回避策を使用して、テキストに色をレンダリングする方法はありますか?

再現可能な例:

library(ggplot2)

pets <- "I like    "

cat(pets)

ggplot() +
  theme_void() +
  annotate("text", x = 1, y = 1, label = pets, size = 15)

RStudioのcat(pets)画面上で動作しますが、最後の行で描画されたグラフィックは次のようになります。

ここに画像の説明を入力

または、ggtext::geom_richtext()同様の白黒の結果と次のエラー メッセージが表示されます。

> library(ggtext)
> ggplot() +
+   theme_void() +
+   annotate("richtext", x = 1, y = 1, label = pets, size = 15)
Warning messages:
1: In text_info(label, fontkey, fontfamily, fontface, fontsize, cache) :
  unable to translate '<U+0001F436>RStudioGD142.6791338582677' to native encoding
2: In text_info(label, fontkey, fontfamily, fontface, fontsize, cache) :
  unable to translate '<U+0001F431>RStudioGD142.6791338582677' to native encoding
3: In text_info(label, fontkey, fontfamily, fontface, fontsize, cache) :
  unable to translate '<U+0001F41F>RStudioGD142.6791338582677' to native encoding
4: In text_info(label, fontkey, fontfamily, fontface, fontsize, cache) :
  unable to translate '<U+0001F422>RStudioGD142.6791338582677' to native encoding
5: In do.call(gList, grobs) :
  unable to translate 'I like <U+0001F436> <U+0001F431> <U+0001F41F> <U+0001F422>' to native encoding
4

1 に答える 1

3

OK、これが私自身の質問に対する答えです。

全体的なアプローチ: 各絵文字を絵文字の画像へのハイパーリンクに変換し、ggtextテキストと画像の組み合わせの新しいバージョンをレンダリングするために使用します。

まず、すべての絵文字のベクトルが必要なので、後でそれらを認識できるようになります。

library(tidyverse)
library(ggtext)
library(rvest)

# test vector
pets <- "I like    "

# the definitive web page with emoji:
unicode <- read_html("https://unicode.org/emoji/charts/full-emoji-list.html")

ut <- unicode %>%
  html_node("table") %>%
  html_table()

# vector of all emoji - purely for recognition purposes
all_emoji <- ut[,3]

次に、Emil Hvitfeldt によるこのページからいくつかの機能をほとんど変更せずに借ります。Emil も同様の課題を抱えていましたが、元の絵文字が単なるテキストであるという問題はありませんでした。

emoji_to_link <- function(x) {
  paste0("https://emojipedia.org/emoji/",x) %>%
    xml2::read_html() %>%
    rvest::html_nodes("tr td a") %>%
    .[1] %>%
    rvest::html_attr("href") %>%
    paste0("https://emojipedia.org/", .) %>%
    xml2::read_html() %>%
    rvest::html_node('div[class="vendor-image"] img') %>%
    rvest::html_attr("src")
}

link_to_img <- function(x, size = 24) {
  paste0("<img src='", x, "' width='", size, "'/>")
}

これらのリンクは絵文字を取得し、Apple Color Emoji フォントによってレンダリングされる絵文字の画像へのハイパーリンクに変換します。ここまでは順調ですが、まずは混合テストから絵文字を抽出する必要があります。これを行うために、さらに2つの関数を書きました

  • 個々のトークン (トークンは個々の絵文字である場合があります) を絵文字に変換するか、変更されていないテキストとして返します。と
  • テキスト文字列をトークン化し、絵文字トークンを画像に変換してから、それらをすべて再度貼り付けます。

これらの 2 つの関数は次のとおりです。

token_to_rt <- function(x){
  if(x %in% all_emoji){
    y <- link_to_img(emoji_to_link(x))
  } else {
    y <- x
  }
  return(y)
}

string_to_rt <- function(x){
  tokens <- str_split(x, " ", simplify = FALSE)[[1]]
  y <- lapply(tokens,  token_to_rt)
  z <- do.call(paste, y)
  return(z)
}

これで必要なものはすべてそろいました。まず、petsベクトルをに変換してから、 と をpets2使用ggplot2ggtextて、見事な色で画面にレンダリングします。

pets2 <- string_to_rt(pets)

ggplot() +
  theme_void() +
  annotate("richtext", x = 1, y = 1, label = pets2, size = 15)

そこにいます:

ここに画像の説明を入力

完全を期すために、R コンソールに表示されたときのキー オブジェクト と の外観を次に示しpetsますpets2all_emoji

> pets
[1] "I like \U0001f436 \U0001f431 \U0001f41f \U0001f422"
> pets2
[1] "I like <img src='https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/237/dog-face_1f436.png' width='24'/> <img src='https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/237/cat-face_1f431.png' width='24'/> <img src='https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/237/fish_1f41f.png' width='24'/> <img src='https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/237/turtle_1f422.png' width='24'/>"
> all_emoji[1:10]
 [1] "face-smiling" "Browser"      "\U0001f600"            "\U0001f603"            "\U0001f604"            "\U0001f601"           
 [7] "\U0001f606"            "\U0001f605"            "\U0001f923"            "\U0001f602"  
于 2020-05-27T10:59:02.947 に答える