gpt4 book ai didi

r - ggplot 中从值到颜色的一致映射

转载 作者:行者123 更新时间:2023-12-02 18:50:49 24 4
gpt4 key购买 nike

我认为我在这里错过了一些非常简单的东西,但我现在无法弄清楚:我想将颜色一致地分配给多个图中的列中的某些值。所以我有这个小标题(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)

但这些颜色与小标题中的颜色不同。所以我尝试遵循此指南: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)

但是结果是一样的。是这样的:

enter image description here

例如,平移幻灯片具有十六进制代码:“#c23b22”,并且应该是柔和的深红色。有人可能知道我在这里缺少什么吗?

最佳答案

考虑一下:


sl <- 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"))

sl$class <- factor( sl$class, levels=unique(sl$class) )

cl <- sl$hex
names(cl) <- paste( sl$class )

ggplot(sl) +
geom_col(aes(x = x,
y = 1,
fill = class)) +
scale_fill_manual( values = cl, na.value = cl["NA"] ) +
geom_text(aes(x = x,
y = 0.5,
label = class),
angle = 90)

通过将class更改为一个因子并为其设置级别,并在scale_fill_manual中使用命名向量作为值,并在其中正确使用na.value,您可能会得到看起来更像的东西预计。

enter image description here

关于r - ggplot 中从值到颜色的一致映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66814794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com