R ggplot条形图:比例\渐变\颜色:需要设置限制和中断

R ggplot条形图:比例\渐变\颜色:需要设置限制和中断,r,ggplot2,data-visualization,R,Ggplot2,Data Visualization,我想为这些数据创建一个条形图,但我需要图例中的颜色渐变,并且通常颜色的比例为1到4。碰巧我的平均值是~2。以下是我尝试过的代码: values= c('Customers', 'Preparedness', 'Integrity', 'ContImprov', 'Teamwork', 'Employees', 'Community', 'Communications') mean = c(2.714,2.800, 2.809, 2.084, 2.02, 2.39, 2.56, 2.48) cou

我想为这些数据创建一个条形图,但我需要图例中的颜色渐变,并且通常颜色的比例为1到4。碰巧我的平均值是~2。以下是我尝试过的代码:

values= c('Customers', 'Preparedness', 'Integrity', 'ContImprov', 'Teamwork', 'Employees', 'Community', 'Communications')
mean = c(2.714,2.800, 2.809, 2.084, 2.02, 2.39, 2.56, 2.48)
count = c(49, 20, 42, 59, 55, 63, 37, 47) 
data = data.frame(Values = values, Mean = mean, Count = count)
我假设“限制和中断”参数可以实现我想要的,但它似乎不会影响我的输出。下面是我得到的图片

更新

亨里克的建议是正确的,我应该使用比例填充梯度


正如@Henrik提到的
填充!=颜色
。因此,将
scale\u color\u gradient
更改为
scale\u fill\u gradient
可以:

ggplot(data, aes(x = Value, y = Count, fill = Mean))
    + geom_bar(stat = 'identity')
    + theme(axis.text.x=element_text(angle=45, hjust=1))
    + guides(fill=guide_legend(title="Average Rank"))
    + ylab("Responses")
    + guides(limits = c(1,4),fill = guide_colorbar(reverse = TRUE))
    + scale_colour_gradient( guide = "colourbar", limits = c(1,4), breaks=c(1,2,3,4))

我相信这就是你要找的。您还可以通过
low=
high=
参数更改使用的颜色。

fill!=颜色
;试着用
scale\u fill\u xxx
来代替。Henrik,类似于scale\u fill\u手册的东西?
ggplot(data, aes(x = Values, y = count, fill = Mean)) +
  geom_bar(stat = 'identity') +
  theme(axis.text.x=element_text(angle=45, hjust=1)) +
  guides(fill=guide_legend(title="Average Rank")) +
  ylab("Responses") +
  guides(limits = c(1,4),fill = guide_colorbar(reverse = TRUE)) +
  scale_fill_gradient(limits = c(1,4), breaks=c(1,2,3,4))