0
data %>% ggplot() + 
  geom_hline(aes(yintercept=Trial, col=Participant2)) + 
  scale_color_discrete(name="NI-1", labels=c("False Alarm", "Hit", "Miss")) + 
  theme_minimal()

有没有办法将以下颜色分配给上面代码行中的标签?

误报-红色

打白

蓝小姐。

谢谢!

4

1 回答 1

0

我们可以使用scale_colour_manual()自定义颜色和标签并将其分配给离散变量的各个级别。

在这里,我使用来自iris示例数据和iris$Species离散变量的数据。

library(ggplot2)

# axis labels
xl <- "Sepal Length"; yl <- "Sepal Width"

# custom colours
my_colours <- c('blue4', 'darkorange', '#00b0a6')
my_colours <- setNames(my_colours, unique(iris$Species))

# custom labels
my_labels <- c('species 1', 'species 2', 'species 3')

# median Sepal.Width per Species
medians_SW <- sapply(split(iris, iris$Species), \(x) median(x$Sepal.Width))

# plot
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species))  +
  geom_point(size = 2, alpha = 0.8) +
  geom_hline(aes(yintercept = medians_SW[1]), color = my_colours[1],
             linetype = 2, size = 0.8, alpha = 0.8) +
  geom_hline(aes(yintercept = medians_SW[2]), color = my_colours[2],
             linetype = 2, size = 0.8, alpha = 0.8) +
  geom_hline(aes(yintercept = medians_SW[3]), color = my_colours[3],
             linetype = 2, size = 0.8, alpha = 0.8) +
  xlab(xl) +  ylab(yl) + theme_minimal() +
  scale_colour_manual(name = 'Species',
                      breaks = unique(iris$Species),
                      values = my_colours,
                      labels = my_labels)

在此处输入图像描述

于 2022-01-12T12:42:36.787 回答