1

我在 geom_point 中将 unicode 形状与合法的 ggplot2 形状结合起来有问题。我有一个包含三种事件类型的变量,我想为死亡级别绘制一把匕首。不幸的是,当我将关卡死亡定义为 unicode 中的 dagger 并将其他事件定义为 ggplot2 中的合法形状时,R 会抛出错误消息:“错误:所有未命名的参数必须是长度 1”。当我用合法形状替换 unicode 时,不会发生此错误。
以下是一些示例数据:

library(tidyverse)

set.seed(123)
id <- 1:10
event <-  sample(c("X", "Y", "Death"), 10, replace = TRUE)
time_to_event <- sample(70:90, 10, replace = TRUE)

data <- data.frame(id, event, time_to_event)

我有一个包含 ID、事件和事件时间的数据集。情节代码如下:

colors <- c("X" = "dodgerblue3", 
            "Y" = "red2",
            "Death" = "black") # Defining the colors for the event type

event_symbols <- c("X" = 19,
                   "Y" = 19, 
                   "Death" = 84) # <-- Problem: When I use the shape 84 (T) the code is plotted. When I use "/u2020", I get the above-mentioned error message

five_day_lines <- seq(0, 90, 5)
day_lines <- 1:90
day_lines <- day_lines[!day_lines %in% five_day_lines]

data$id <- factor(data$id, levels = data$id[order(data$time_to_event, decreasing = T)])

ggplot(data = data) +
  
  geom_hline(yintercept = c(seq(0, 90, 5)), color = "grey", alpha = .35) +
  geom_hline(yintercept = day_lines, color = "grey", alpha = .25) +
  
  geom_point(aes(x = id, y = time_to_event, shape = event, color = event), size = 3) +
  
  scale_y_continuous(limits = c(0,90), breaks = c(seq(0, 90, 5)), name = "Days after study inclusion") + 
  scale_x_discrete(name = "ID") +
  coord_flip() +
  scale_color_manual(values = colors, breaks = c()) +
  scale_shape_manual(values = event_symbols, breaks = c("X", "Y", "Death"),
                     guide = guide_legend(override.aes = list(color = c("dodgerblue3", "red2", "black")))) +
  theme(legend.position = "bottom", 
        legend.title = element_text(size=12, face="bold"),
        panel.background = element_blank(),
        legend.background = element_blank()) +
  labs(color="Medication", shape="Event type")

我还尝试将所有形状定义为 unicode 形状。然后,错误消息没有出现,但其他 unicode 形状未正确显示。老实说,如果有来自 ggplot2 (19) 的法律圈和来自 unicode ("/u2020") 的匕首,那就太好了。我在网上搜索了很多,但找不到合适的答案。

当有人知道如何处理它时,我将不胜感激。

谢谢弗洛里安

4

1 回答 1

1

不幸的是,我担心这无法实现。从文档

形状有五种类型的值:

  • [0,25] 中的整数
  • 形状的名称
  • 单个字符,将该字符用作绘图符号。

因此,看起来好像不可能混合这些类型。相反,我建议对其他形状也使用 unicode 符号,例如"\u25CF"圆形

library(tidyverse)

set.seed(123)
id <- 1:10
event <-  sample(c("X", "Y", "Death"), 10, replace = TRUE)
time_to_event <- sample(70:90, 10, replace = TRUE)

data <- data.frame(id, event, time_to_event)

  colors <- c("X" = "dodgerblue3", 
              "Y" = "red2",
              "Death" = "black") # Defining the colors for the event type

event_symbols <- c("X" = "\u25CF",
                   "Y" =  "\u25CF", 
                   "Death" = "\u2020") # <-- Problem: When I use the shape 84 (T) the code is plotted. When I use "/u2020", I get the above-mentioned error message

five_day_lines <- seq(0, 90, 5)
day_lines <- 1:90
day_lines <- day_lines[!day_lines %in% five_day_lines]

data$id <- factor(data$id, levels = data$id[order(data$time_to_event, decreasing = T)])

ggplot(data = data) +
  
  geom_hline(yintercept = c(seq(0, 90, 5)), color = "grey", alpha = .35) +
  geom_hline(yintercept = day_lines, color = "grey", alpha = .25) +
  
  geom_point(aes(x = id, y = time_to_event, shape = event, color = event), size = 3) +
  
  scale_y_continuous(limits = c(0,90), breaks = c(seq(0, 90, 5)), name = "Days after study inclusion") + 
  scale_x_discrete(name = "ID") +
  coord_flip() +
  scale_color_manual(values = colors, breaks = c()) +
  scale_shape_manual(values = event_symbols, breaks = c("X", "Y", "Death"),
                     guide = guide_legend(override.aes = list(color = c("dodgerblue3", "red2", "black")))) +
  theme(legend.position = "bottom", 
        legend.title = element_text(size=12, face="bold"),
        panel.background = element_blank(),
        legend.background = element_blank()) +
  labs(color="Medication", shape="Event type")

reprex 包于 2021-06-06 创建 (v2.0.0 )

于 2021-06-06T13:25:44.167 回答