2014-11-14 217 views
0

也許這是一個非常基本的問題,但我是一個ggplot和R初學者。ggplot更改填充顏色而不會丟失顏色梯度

我使用這個命令來獲取barplot:

ggplot(data=melt, aes(x=variable, y=value, fill=value)) + 
    geom_bar(width=.8, stat="identity") + 
    xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") + 
    theme(plot.title=element_text(face="bold", size=12)) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) + 
    theme(axis.text.y = element_text(size=10)) 

我想改變barplot的顏色,但保持取決於值列的顏色漸變。我試過,但我失去了梯度:

ggplot(data=melt, aes(x=variable, y=value, fill=value)) + 
    geom_bar(width=.8, stat="identity", fill="red") + 
    xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") + 
    theme(plot.title=element_text(face="bold", size=12)) + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) + 
    theme(axis.text.y = element_text(size=10)) 

的數據是簡單,只有兩列(變量 - 值):

variable value 
1 nu73 13576.49 
2 nu73t 10891.88 
3 nu81 12673.33 
4 nu81t 12159.91 
5 nu83 12570.82 
6 nu83t 11828.04 

謝謝你們提前

回答

1

你想調整比例,特別是填充顏色的連續比例,因此功能scale_fill_continuous()

ggplot(data = melt, aes(x = variable, y = value, fill = value)) + 
    geom_bar(width = .8, stat = "identity") + 
    labs(x = "Samples", y = "Expression", title = "Gapdh") + 
    theme(plot.title = element_text(face = "bold", size = 12), 
      axis.text.x = element_text(angle = 45, hjust = 1, size = 10), 
      axis.text.y = element_text(size = 10)) + 
    scale_fill_continuous(low = "firebrick4", high = "firebrick1") 

(我稍微修改您的繪製代碼:你可以用多個參數調用一次theme,我覺得labs不是一堆個人標籤調用的更好)

另一個選擇是使用調色板從RColorBrewer包(這是包含在ggplot2)。 scale_fill_brewer()如果對於離散的色標,則可以將它們「提取」成與scale_fill_distiller()連續的比例。例如

scale_fill_distiller(type = "seq", palette = "Reds") 

要查看所有可用的比例,請運行RColorBrewer::display.brewer.all()

+0

是的!非常感謝。 – cucurbit 2014-11-14 17:17:03