R中相同颜色的宽范围

时间:2014-01-29 09:51:12

标签: r colors color-palette

是否可以创建大量的不同种类的“红色”颜色。为了更好地理解我期待跟随,但我希望有“红色”或“红黑色”而不是灰色。

mypalette <- rev(grey.colors(10000, start = 0.1, end = 0.5, gamma = 4))
plot(1:length(mypalette),1:length(mypalette), col=mypalette, pch=16)

我知道的颜色包颜色范围有限。任何想法都将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果我了解您的需求,请尝试colorRampPalette。它返回一个函数,用于在您指定的两个颜色之间输出所请求的颜色数。

reds <- colorRampPalette(c("black","red"))
reds(5)
[1] "#000000" "#3F0000" "#7F0000" "#BF0000" "#FF0000"

答案 1 :(得分:0)

以下是一些ggplot替代方案

library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100), z2 = factor(1:5))

# colour set by continuous variable
ggplot(data = df, aes(x = x, y = y, colour = z)) +
  geom_point() +
  scale_colour_gradient(low = "red", high = "white")

enter image description here

library(RColorBrewer)
ggplot(data = df, aes(x = x, y = y, colour = z)) +
  geom_point() +
  scale_colour_gradientn(colours = brewer.pal(5, "Reds"))

enter image description here

# colour set by discrete variable
ggplot(data = df, aes(x = x, y = y, colour = z2)) +
  geom_point() +
  scale_colour_brewer(palette = "Reds")

enter image description here