如何从 ggplot 图形中提取 viridis 调色板中使用的十六进制颜色代码?
How to extract from a ggplot figure which Hex color codes were used in the viridis color palette?
在 ggplot
中制作图形并使用 viridis
调色板后,我想获得为该特定图随机选择的所有颜色的十六进制颜色代码列表。这可能吗?
这是数据:
df = data.frame(Cell = c(rep("13a",5), rep("1b",5), rep("5b",5)),
condition = rep(c("a","b","c","d","e"), 3),
variable = c(58,55,36,29,53, 57,53,54,52,52, 45,49,48,46,45))
剧情:
library(ggplot2)
library(viridis)
windows(width=4, height=4 )
ggplot(df, aes(x = condition, y = variable, group = Cell, color = Cell)) +
geom_point(aes(color = Cell))+
geom_line()+
scale_color_viridis(discrete=TRUE)
您可以手动重新创建用于 scales::viridis_pal()(n)
调用的十六进制(由@Gregor 提议):
scales::viridis_pal()(length(unique(df$Cell)))
[1] "#440154FF" "#21908CFF" "#FDE725FF"
但是,您也可以使用 ggplot_build
:
访问任何 ggplot
对象的基础数据
我们先将您的绘图保存为 gg
:
gg <- ggplot(df, aes(x = condition, y = variable, group = Cell, color = Cell)) +
geom_point(aes(color = Cell))+
geom_line()+
scale_color_viridis(discrete=TRUE)
现在访问底层组件:
ggplot_build(gg)
因为我们真的只对数据感兴趣:
ggplot_build(gg)$data
这给了我们:
[[1]]
colour x y group PANEL shape size fill alpha stroke
1 #440154FF 1 58 1 1 19 1.5 NA NA 0.5
2 #440154FF 2 55 1 1 19 1.5 NA NA 0.5
3 #440154FF 3 36 1 1 19 1.5 NA NA 0.5
4 #440154FF 4 29 1 1 19 1.5 NA NA 0.5
5 #440154FF 5 53 1 1 19 1.5 NA NA 0.5
6 #21908CFF 1 57 2 1 19 1.5 NA NA 0.5
7 #21908CFF 2 53 2 1 19 1.5 NA NA 0.5
8 #21908CFF 3 54 2 1 19 1.5 NA NA 0.5
9 #21908CFF 4 52 2 1 19 1.5 NA NA 0.5
10 #21908CFF 5 52 2 1 19 1.5 NA NA 0.5
11 #FDE725FF 1 45 3 1 19 1.5 NA NA 0.5
12 #FDE725FF 2 49 3 1 19 1.5 NA NA 0.5
13 #FDE725FF 3 48 3 1 19 1.5 NA NA 0.5
14 #FDE725FF 4 46 3 1 19 1.5 NA NA 0.5
15 #FDE725FF 5 45 3 1 19 1.5 NA NA 0.5
[[2]]
colour x y group PANEL size linetype alpha
1 #440154FF 1 58 1 1 0.5 1 NA
2 #440154FF 2 55 1 1 0.5 1 NA
3 #440154FF 3 36 1 1 0.5 1 NA
4 #440154FF 4 29 1 1 0.5 1 NA
5 #440154FF 5 53 1 1 0.5 1 NA
6 #21908CFF 1 57 2 1 0.5 1 NA
7 #21908CFF 2 53 2 1 0.5 1 NA
8 #21908CFF 3 54 2 1 0.5 1 NA
9 #21908CFF 4 52 2 1 0.5 1 NA
10 #21908CFF 5 52 2 1 0.5 1 NA
11 #FDE725FF 1 45 3 1 0.5 1 NA
12 #FDE725FF 2 49 3 1 0.5 1 NA
13 #FDE725FF 3 48 3 1 0.5 1 NA
14 #FDE725FF 4 46 3 1 0.5 1 NA
15 #FDE725FF 5 45 3 1 0.5 1 NA
在 ggplot
中制作图形并使用 viridis
调色板后,我想获得为该特定图随机选择的所有颜色的十六进制颜色代码列表。这可能吗?
这是数据:
df = data.frame(Cell = c(rep("13a",5), rep("1b",5), rep("5b",5)),
condition = rep(c("a","b","c","d","e"), 3),
variable = c(58,55,36,29,53, 57,53,54,52,52, 45,49,48,46,45))
剧情:
library(ggplot2)
library(viridis)
windows(width=4, height=4 )
ggplot(df, aes(x = condition, y = variable, group = Cell, color = Cell)) +
geom_point(aes(color = Cell))+
geom_line()+
scale_color_viridis(discrete=TRUE)
您可以手动重新创建用于 scales::viridis_pal()(n)
调用的十六进制(由@Gregor 提议):
scales::viridis_pal()(length(unique(df$Cell)))
[1] "#440154FF" "#21908CFF" "#FDE725FF"
但是,您也可以使用 ggplot_build
:
ggplot
对象的基础数据
我们先将您的绘图保存为 gg
:
gg <- ggplot(df, aes(x = condition, y = variable, group = Cell, color = Cell)) +
geom_point(aes(color = Cell))+
geom_line()+
scale_color_viridis(discrete=TRUE)
现在访问底层组件:
ggplot_build(gg)
因为我们真的只对数据感兴趣:
ggplot_build(gg)$data
这给了我们:
[[1]]
colour x y group PANEL shape size fill alpha stroke
1 #440154FF 1 58 1 1 19 1.5 NA NA 0.5
2 #440154FF 2 55 1 1 19 1.5 NA NA 0.5
3 #440154FF 3 36 1 1 19 1.5 NA NA 0.5
4 #440154FF 4 29 1 1 19 1.5 NA NA 0.5
5 #440154FF 5 53 1 1 19 1.5 NA NA 0.5
6 #21908CFF 1 57 2 1 19 1.5 NA NA 0.5
7 #21908CFF 2 53 2 1 19 1.5 NA NA 0.5
8 #21908CFF 3 54 2 1 19 1.5 NA NA 0.5
9 #21908CFF 4 52 2 1 19 1.5 NA NA 0.5
10 #21908CFF 5 52 2 1 19 1.5 NA NA 0.5
11 #FDE725FF 1 45 3 1 19 1.5 NA NA 0.5
12 #FDE725FF 2 49 3 1 19 1.5 NA NA 0.5
13 #FDE725FF 3 48 3 1 19 1.5 NA NA 0.5
14 #FDE725FF 4 46 3 1 19 1.5 NA NA 0.5
15 #FDE725FF 5 45 3 1 19 1.5 NA NA 0.5
[[2]]
colour x y group PANEL size linetype alpha
1 #440154FF 1 58 1 1 0.5 1 NA
2 #440154FF 2 55 1 1 0.5 1 NA
3 #440154FF 3 36 1 1 0.5 1 NA
4 #440154FF 4 29 1 1 0.5 1 NA
5 #440154FF 5 53 1 1 0.5 1 NA
6 #21908CFF 1 57 2 1 0.5 1 NA
7 #21908CFF 2 53 2 1 0.5 1 NA
8 #21908CFF 3 54 2 1 0.5 1 NA
9 #21908CFF 4 52 2 1 0.5 1 NA
10 #21908CFF 5 52 2 1 0.5 1 NA
11 #FDE725FF 1 45 3 1 0.5 1 NA
12 #FDE725FF 2 49 3 1 0.5 1 NA
13 #FDE725FF 3 48 3 1 0.5 1 NA
14 #FDE725FF 4 46 3 1 0.5 1 NA
15 #FDE725FF 5 45 3 1 0.5 1 NA