在ggplot 2中查看现有调色板中的特定颜色名称

7

1
它们来自colorbrewer,在那里您可以查找十六进制或RGB等值。 "Set1"是第六个(具有讽刺意味的)定性调色板。 - alistaire
1个回答

12

你可以通过ggplot_build找到代码。

# fake data
df <- data.frame(x=1:8, y=1, col=letters[1:8])

# Construct the plot
g <- ggplot(df, aes(x=x, y=y, color=col)) + geom_point(size=5) +
  scale_color_brewer(palette="Set1")

g

在此输入图像描述

# Retrieve the color
colors <- ggplot_build(g)$data[[1]]$colour

# Double check
plot(df$x, df$y, col=colors, pch=20, cex=5)

在此输入图片描述

# color 4 and 5
colors[4:5]
[1] "#984EA3" "#FF7F00"

10
也可以使用 RColorBrewer::brewer.pal(8, "Set1") 函数。该函数会返回一个包含 8 种颜色的向量,用于数据可视化等应用场景。 - user20650

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接