微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 unicode 形状与合法的 ggplot2 形状结合时出错

如何解决将 unicode 形状与合法的 ggplot2 形状结合时出错

我在将 unicode 形状与 geom_point 中的合法 ggplot2 形状组合时遇到问题。我有一个包含三种事件类型的变量,我想为死亡级别绘制一把匕首。不幸的是,当我将关卡死亡定义为 unicode 中的匕首并将其他事件定义为来自 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,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,5)),color = "grey",alpha = .35) +
  geom_hline(yintercept = day_lines,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,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",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") 的匕首会很好。我在网上搜索了很多,但找不到合适的答案。

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

谢谢 弗洛里安

解决方法

不幸的是,我担心这无法实现。来自docs

形状采用五种类型的值:

  • [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,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,5)),color = "grey",alpha = .35) +
  geom_hline(yintercept = day_lines,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,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",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 package (v2.0.0) 于 2021 年 6 月 6 日创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。