在ggplot中从深到浅订购顺序调色板

时间:2018-09-08 02:48:46

标签: r ggplot2

我在订购自定义的顺序调色板时遇到困难,因此绘图中的最高值最暗,最低值最亮。

我的数据在下面。

dput(preseason18)
#> Error in dput(preseason18): object 'preseason18' not found
structure(list(Week.Number = structure(1:16, .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16"), class = "factor"), Distance.18 = c(5331.83038, 
14084.08602, 12219.423585, 14406.407445, 5032.74848, 10820.094835, 
16935.546075, 15387.590625, 16195.21247, 20012.09881, 14057.385255, 
5127.14891, 16241.98523, 12793.21837, 10526.785375, 6014.43878
), HIR.18 = c(1098.56001, 4093.010015, 4372.84498, 4074.22002, 
709.70499, 2460.04999, 5037.77501, 5521.029965, 5463.410025, 
6761.34502, 3953.20997, 1189.89, 3663.69006, 2333.005005, 2289.38001, 
1069.740005), V6.18 = c(0, 40.77, 63.505, 112.63, 52.395, 56.795, 
211.115, 75.52, 215.059995, 121.725, 57.64, 15.35, 140.34, 15.615, 
85.66, 31.815)), .Names = c("Week.Number", "Distance.18", "HIR.18", 
"V6.18"), row.names = c(NA, -16L), class = "data.frame")

我使用以下代码自定义了调色板。

getPalette = colorRampPalette(brewer.pal(9, "Oranges"))
getPalette(colourCount)

然后我用下面的代码生成下面的图。

plot <- ggplot(preseason18) +
  geom_col(aes(x = reorder(Week.Number, Distance.18), 
               y = Distance.18, 
               fill = Week.Number)) +
  coord_flip() +
  scale_fill_manual(values = getPalette(colourCount)) +
  theme_classic()
plot + guides(fill = FALSE)

enter image description here

如何对颜色进行排序,使它们从暗(Distance.18的最高值)到亮(Distance.18的最低值)?与scale_fill_brewer有什么关系?

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

scale_fill_brewer提供了离散的ColorBrewer比例; scale_fill_distiller对其进行插值以连续使用。您还需要将填充映射到长度变量(Distance.18)。

library(ggplot2)

ggplot(preseason18) +
    geom_col(aes(x = reorder(Week.Number, Distance.18), 
                 y = Distance.18, 
                 fill = Distance.18)) +
    coord_flip() +
    scale_fill_distiller(palette = "Oranges", guide = FALSE, direction = 1) +
    theme_classic()