小编sci*_*sis的帖子

如何在ggplot中手动设置geom_bar填充颜色

我正在尝试使用ggplot创建几个图形.这些图是一系列条形图,它们一起描述了一条线以及例子 (BTW,是的,我意识到调色板是丑陋的,它是色盲友好的,这对我的观众很重要)

我的问题是我需要制作其中的几个图表,并希望颜色在所有图形中保持一致.由于"Type"变量在我将要使用的几个数据集中以不同的顺序出现,我需要为每种类型手动设置颜色.我认为这个问题:如何在ggplot2直方图中手动填充颜色会得到答案,但是当我尝试时,它会将图例中的名称更改为颜色的十六进制定义,但颜色本身会返回到ggplot的默认值调色板.

这是我到目前为止的代码:

  cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

 ggplot()+
    scale_fill_manual(values=cbbPalette)+
    geom_bar(data=subset(eten, Type=="Waste Wood"), aes(x=Tprod, y=acost, fill=cbbPalette[1], width=MGGEY+25), stat="identity")+
    geom_bar(data=subset(eten, Type=="Agricultural Residue"), aes(x=Tprod, y=acost, fill=cbbPalette[2], width=MGGEY+25), stat="identity")+
    geom_bar(data=subset(eten, Type=="Forest Residue"), aes(x=Tprod, y=acost, fill=cbbPalette[3], width=MGGEY+25), stat="identity")+
    geom_bar(data=subset(eten, Type=="Herbaceous Energy Crop"), aes(x=Tprod, y=acost, fill=cbbPalette[4], width=MGGEY+25), stat="identity")+
    geom_bar(data=subset(eten, Type=="MSW"), aes(x=Tprod, y=acost, fill=cbbPalette[5], width=MGGEY+25), stat="identity")+
    scale_y_continuous("Average Cost", labels = dollar, expand=c(0,0))+
    scale_x_continuous("Million Gallons of Gasoline Equivalent", expand=c(0,0))+
    theme(legend.position="bottom", panel.background=element_rect(colour = NA, fill = "white"), axis.line=element_line(), panel.grid.major.y=element_line(colour="black"), …
Run Code Online (Sandbox Code Playgroud)

r colors ggplot2

6
推荐指数
2
解决办法
4万
查看次数

标签 统计

colors ×1

ggplot2 ×1

r ×1