Scale_color_manual未按预期工作

时间:2014-10-23 00:01:04

标签: r charts plot ggplot2

我使用以下代码创建条形图:

temp1 <- melt(final,id='Time')
p <- ggplot(temp1, aes(x=Time, y=value, fill=variable)) +
  geom_bar(stat="identity", position = "fill") +
  scale_y_continuous(labels = percent_format()) +
  ggtitle('Cost Structure Breakdown') + theme_bw() + ylab('Percent Spread') +
  theme(panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.major.x=element_line(color='grey90',linetype='dashed'),
        panel.grid.major.y=element_line(color='grey90',linetype='dashed'),
        plot.title=element_text(size=20),
        axis.text=element_text(size=15),
        legend.text=element_text(size=15),
        legend.key=element_blank(),
        legend.title=element_blank()) +
  scale_color_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6"))
p

scale_color_manual的存在似乎对结果图没有任何影响,即使我删除了scale_color_manual,它也保持不变。我的数据框final有10个变量,我在melt上使用Time。因此,我使用了9种颜色作为情节。可以找到数据集本身here

对此的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:3)

没有可重复的数据。因此,我在这里创建了一个简单的数据。我还简化了OP的代码。这里需要的是scale_fill_manual

mydf <- data.frame(time = letters[1:3],
                   variable = LETTERS[1:3],
                   value = runif(3, 10, 15),
                   stringsAsFactors = FALSE)


ggplot(mydf, aes(x=time, y=value, fill=variable)) +
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a"))

enter image description here

相关问题