2017-06-16 177 views
0

我用繪製在彼此的頂部的兩個柱狀圖如下:改變顏色GGPLOT2

library(ggplot2) 
library(reshape2) 


x = c("type1", "type2", "type3") 
y1 = c("1","2","3") 
y2 = c("2","3","4") 

to_plot <- data.frame(x=x,y1=y1,y2=y2) 
melted<-melt(to_plot, id="x") 

ggplot() + geom_bar(data=melted,aes(x=x,y=value,fill=variable),stat="identity",position = "identity", alpha=.3) 

這給了我下面的輸出: enter image description here

反正我有可以改變條形圖的顏色嗎?例如,我想Y1圖是一樣的灰色作爲背景

+1

請仔細閱讀[如何使R中一個偉大的可重複的例子? ](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Masoud

+0

好的,我做了一個可重複的例子@Masoud – cosmictypist

+0

'scale_fill_manual(「legend」,我的 – Masoud

回答

1
ggplot() + geom_bar(data=melted,aes(x=x,y=value,fill=variable), 
        stat="identity",position = "identity", alpha=.3) + 
    scale_fill_manual(values=c("#955599", "#E63F00")) 

現在,您可以用調色板(玩 - ;

+0

:D:D謝謝! lightblue「))+ theme_gray()'如果你不想玩 – cosmictypist

+0

'myplot + scale_fill_manual(values = c(「gray」,「y1」=「grey」,「y2」=「orange」))' – Masoud

+0

哈哈。謝謝@Masoud – cosmictypist