ggplot (geom_point) 中的自定义形状 - 使用大脑表情符号🧠

问题描述 投票:0回答:1

这是这个问题的后续(第 2 部分):ggplot (geom_point) 中的自定义形状(这是 第 1 部分

这提供了一个解决方案来生成头骨💀(或心❤)形式的自定义 ggplot2 形状:

library(ggplot2)
df <- read.table(text="x y
                 1    3
                 2    4
                 3    6
                 4    7", header=TRUE) 

ggplot(data = df, aes(x =x, y=y)) +  
  geom_point(shape="\u2764",
             colour = "red",
             fill = "white",
             size = 6)

创建于 2023-12-25,使用 reprex v2.0.2

但是出现了一个问题,因为我实际上想绘制一个大脑🧠...

ggplot(data = df, aes(x =x, y=y)) +  
  geom_point(shape="\u1F9E0",
             colour = "red",
             fill = "white",
             size = 6)
#> Error in `geom_point()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `translate_shape_string()`:
#> ! Shape aesthetic contains invalid value: "ᾞ0"
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(input = base::quote("super-sable_reprex.R"))
#>  13.   └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14.     └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15.       └─knitr:::process_file(text, output)
#>  16.         ├─knitr:::handle_error(...)
#>  17.         │ └─base::withCallingHandlers(...)
#>  18.         ├─base::withCallingHandlers(...)
#>  19.         ├─knitr:::process_group(group)
#>  20.         └─knitr:::process_group.block(group)
#>  21.           └─knitr:::call_block(x)
#>  22.             └─knitr:::block_exec(params)
#>  23.               └─knitr:::eng_r(options)
#>  24.                 ├─knitr:::in_input_dir(...)
#>  25.                 │ └─knitr:::in_dir(input_dir(), expr)
#>  26.                 └─knitr (local) evaluate(...)
#>  27.                   └─evaluate::evaluate(...)
#>  28.                     └─evaluate:::evaluate_call(...)
#>  29.                       ├─evaluate (local) handle(...)
#>  30.                       │ └─base::try(f, silent = TRUE)
#>  31.                       │   └─base::tryCatch(...)
#>  32.                       │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  33.                       │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  34.                       │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  35.                       ├─base::withCallingHandlers(...)
#>  36.                       ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  37.                       └─knitr (local) value_fun(ev$value, ev$visible)
#>  38.                         └─knitr (local) fun(x, options = options)
#>  39.                           ├─base::withVisible(knit_print(x, ...))
#>  40.                           ├─knitr::knit_print(x, ...)
#>  41.                           └─knitr:::knit_print.default(x, ...)
#>  42.                             └─evaluate (local) normal_print(x)
#>  43.                               ├─base::print(x)
#>  44.                               └─ggplot2:::print.ggplot(x)
#>  45.                                 ├─ggplot2::ggplot_gtable(data)
#>  46.                                 └─ggplot2:::ggplot_gtable.ggplot_built(data)
#>  47.                                   └─ggplot2:::by_layer(...)
#>  48.                                     ├─rlang::try_fetch(...)
#>  49.                                     │ ├─base::tryCatch(...)
#>  50.                                     │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  51.                                     │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  52.                                     │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  53.                                     │ └─base::withCallingHandlers(...)
#>  54.                                     └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  55.                                       └─l$draw_geom(d, layout)
#>  56.                                         └─ggplot2 (local) draw_geom(..., self = self)
#>  57.                                           └─self$geom$draw_layer(...)
#>  58.                                             └─ggplot2 (local) draw_layer(..., self = self)
#>  59.                                               └─base::lapply(...)
#>  60.                                                 └─ggplot2 (local) FUN(X[[i]], ...)
#>  61.                                                   ├─rlang::inject(self$draw_panel(data, panel_params, coord, !!!params))
#>  62.                                                   └─self$draw_panel(data, panel_params, coord, na.rm = FALSE)
#>  63.                                                     └─ggplot2 (local) draw_panel(..., self = self)
#>  64.                                                       └─ggplot2:::translate_shape_string(data$shape)
#>  65.                                                         └─cli::cli_abort("Shape aesthetic contains invalid value{?s}: {.val {bad_string}}")
#>  66.                                                           └─rlang::abort(...)

创建于 2023-12-25,使用 reprex v2.0.2

有可能吗?有人可以解释这个特定表情符号的问题或者为什么它显然不受支持吗?

r ggplot2 unicode shapes
1个回答
0
投票

您需要代码点的不同表示方式

这与 R 如何解释

"\u1f9e0"
有关。当您将值传递给
shape
中的
geom_point()
参数时,由
ggplot2:::translate_shape_string()
处理。该函数主要执行以下操作:

  1. 如果参数只有一个字符,例如
    "\u2764"
    "❤"
    ,它返回要绘制的值。
  2. 否则它将与 R 的
    pch
    符号 匹配,例如
    "triangle down filled" = 25

问题在于,虽然 R 可以正确确定

"\u2764"
是一个字符,但对于
"\u1f9e0"
却不能这样做:

nchar("\u2764", type = "chars") # heart
# [1] 1
nchar("\u1f9e0", type = "chars") # brain
# [1] 2

因此将其视为

pch
名称,该名称无法与值匹配并引发错误。然而,让我们看看当我们向 R 提供大脑符号的十进制值时,R 如何打印代码点:

intToUtf8(strtoi("0x1f9e0"))
# [1] "\U{01f9e0}"
nchar("\U{01f9e0}")
# [1] 1

如果我们使用这种表示,我们将得到期望的结果:

ggplot(data = df, aes(x = x, y = y)) +
    geom_point(
        shape = "\U{01f9e0}",
        colour = "red",
        size = 10
    ) +
    theme_bw()

© www.soinside.com 2019 - 2024. All rights reserved.