r - 更改直方图中的调色板

标签 r colors histogram ggplot2

我正在尝试更改直方图的颜色,但不知道该怎么做,这是我的代码:

qplot(user, count, data=count_group, geom="histogram", fill=group,
            xlab = "users", ylab="count", 
            main="Users")+
    opts(axis.text.x=theme_text(angle=90, hjust=0, size=7))

这是我得到的直方图,但默认颜色太亮了,

我想使用像 this 这样的颜色

我试图添加该行,但没有奏效。
  scale_fill_brewer(palette = palette)

最佳答案

如果您想将 Brewer Set1 与这么多组一起使用,您可以执行以下操作:

library(ggplot2)

count_group <- data.frame(user=factor(rep(1:50, 2)), 
                          count=sample(100, 100, replace=T), 
                          group=factor(rep(LETTERS[1:20], 5)))

library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(9, "Set1"))
ngroups <- length(unique(count_group$group))
qplot(user, count, data=count_group, geom="histogram", fill=group,
      xlab = "users", ylab="count") +
      opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) +
      scale_fill_manual(values = cols(ngroups))


编辑

您可以创建和使用多个 colorRampPalette s,例如将蓝色分配给 A 到 J 组,将红色分配给 K 到 T 组:
blues <- colorRampPalette(c('dark blue', 'light blue'))
reds <- colorRampPalette(c('pink', 'dark red'))

qplot(user, count, data=count_group, geom="histogram", fill=group,
      xlab = "users", ylab="count") +
        opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) +
        scale_fill_manual(values = c(blues(10), reds(10)))
# blues(10) and reds(10) because you want blues for the first ten
#  groups, and reds thereafter. Each of these functions are equivalent
#  to providing vectors containing ten hex colors representing a gradient
#  of blues and a gradient of reds.

关于r - 更改直方图中的调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9409937/

相关文章:

r - 如何将R数据帧导出为代码共享?

java - 设置 Java 文本框中的文本颜色

C++ 加载和循环图像像素(JPEG 格式)

javascript - href 链接上的 jQuery 颜色循环(超出最大调用堆栈大小和 Firefox 问题。)

python - 使用python制作加权直方图?

python - 将直方图放入 tkinter 框架中

r - 在 R 中生成数字

r - 在第一个非 NA 值之后递归填充元素

r - 如何根据多面条形图的分类变量因子进行着色?

python - 加权直方图的样本