0

我知道有几个问题已经提出了这个问题,但是我已经实施了其中一些的解决方案并且没有任何运气。

我以 mtcars 为例:

mtcars_new <- mtcars %>% 
    mutate(
        colour = case_when(
            gear == 3 ~ "#FFFFFF",
            gear == 4 ~ "#FF7000",
            gear == 5 ~ "#85AEFF"
        )
    )

hm <- ggplot(mtcars_new, aes(cyl, carb, fill = colour)) + 
    geom_tile() +
    scale_colour_identity()
    

这将产生此图,其中颜色与十六进制代码不匹配:

热图

我也尝试了以下方法,但没有奏效:

hm <- ggplot(mtcars_new, aes(cyl, carb, fill = colour)) + 
    geom_tile() +
    scale_colour_identity()

hm <- ggplot(mtcars_new, aes(cyl, carb), fill = colour)) + 
    geom_tile()

从 df 中获取十六进制代码并使用它来定义颜色的最佳方法是什么?

4

0 回答 0