【发布时间】:2021-06-23 03:44:17
【问题描述】:
我想我在这里遗漏了一些非常简单的东西,但我现在无法弄清楚:
我想始终如一地将颜色分配给跨多个图的列中的某些值。
所以我有这个小标题(sl):
# A tibble: 15 x 3
class hex x
<chr> <chr> <int>
1 translational slide #c23b22 1
2 rotational slide #AFC6CE 2
3 fast flow-type #b7bf5e 3
4 complex #A6CEE3 4
5 area subject to rockfall/topple #1F78B4 5
6 fall-type #B2DF8A 6
7 n.d. #33A02C 7
8 NA #FB9A99 8
9 area subject to shallow-slides #E31A1C 9
10 slow flow-type #FDBF6F 10
11 topple #FF7F00 11
12 deep-seated movement #CAB2D6 12
13 subsidence #6A3D9A 13
14 areas subject to subsidence #FFFF99 14
15 area of expansion #B15928 15
这应该重新创建它:
structure(list(class = c("translational slide", "rotational slide",
"fast flow-type", "complex", "area subject to rockfall/topple",
"fall-type", "n.d.", NA, "area subject to shallow-slides", "slow flow-type",
"topple", "deep-seated movement", "subsidence", "areas subject to subsidence",
"area of expansion"), hex = c("#c23b22", "#AFC6CE", "#b7bf5e",
"#A6CEE3", "#1F78B4", "#B2DF8A", "#33A02C", "#FB9A99", "#E31A1C",
"#FDBF6F", "#FF7F00", "#CAB2D6", "#6A3D9A", "#FFFF99", "#B15928"
), x = 1:15), row.names = c(NA, -15L), class = c("tbl_df", "tbl",
"data.frame"))
现在,如果每个类的十六进制代码(现在仅用于可视化目的),我想用颜色中的条绘制每个类。所以我做了:
ggplot(sl) +
geom_col(aes(x = x,
y = 1,
fill = class)) +
scale_fill_manual(values = sl$hex) +
geom_text(aes(x = x,
y = 0.5,
label = class),
angle = 90)
但这些不是 tibble 中的颜色。 所以我试着按照这个指南:How to assign colors to categorical variables in ggplot2 that have stable mapping? 并创建了这个:
# create the color palette
mycols = sl$hex
names(mycols) = sl$class
然后用它绘制
ggplot(sl) +
geom_col(aes(x = x,
y = 1,
fill = class)) +
scale_fill_manual(values = mycols) +
geom_text(aes(x = x,
y = 0.5,
label = class),
angle = 90)
但结果是一样的。是这样的:
例如,平移幻灯片具有十六进制代码:“#c23b22”,应该是柔和的深红色。 任何人都可能知道我在这里缺少什么?
【问题讨论】:
标签: r ggplot2 color-palette