更改直方图中的调色板

Mal*_*ina 4 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))
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我想用的颜色一样,

我试图添加该行,但它没有用.

  scale_fill_brewer(palette = palette)
Run Code Online (Sandbox Code Playgroud)

jba*_*ums 11

如果你想将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))
Run Code Online (Sandbox Code Playgroud)

Brewer在ggplot中设置了许多组


编辑

您可以创建和使用多个colorRampPalettes,例如将蓝色分配给组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.
Run Code Online (Sandbox Code Playgroud)

蓝色和红色的ggplot