我正在尝试更改直方图的颜色,但不知道该怎么做,这是我的代码:
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/