2011-10-13 229 views
3

我想繪製與ggplot2熱圖,我想調整顏色條和增加字體。熱圖ggplot2顏色斜坡(scale_fill_gradient)

這裏是代碼的相關部分:

g <- ggplot(data=melt.m) 
g2 <- g+geom_rect(aes(xmin=colInd-1, xmax=colInd, 
    ymin=rowInd-1, ymax=rowInd, fill=value)) 

g2 <- g2+scale_x_continuous('beta', breaks=c(1, ceiling(cols/2), rows)-0.5, 
    labels=c(1,ceiling(cols/2), rows)) 
g2 <- g2+scale_y_continuous('alpha', breaks=c(1, ceiling(rows/2), rows)-0.5, 
    labels=c(1, ceiling(rows/2), rows)) 

g2 <- g2+opts(panel.grid.minor=theme_line(colour=NA), 
    panel.grid.major=theme_line(colour=NA), 
    panel.background=theme_rect(fill=NA, colour=NA), 
    axis.text.x=theme_text(size=30), 
    axis.text.y=theme_text(size=30, angle=90), 
    axis.title.x=theme_text(size=30), 
    axis.title.y=theme_text(size=30, angle=90), title = title) 

heatscale <- c(low='ghostwhite', high='steelblue') 

g2 <- g2+scale_fill_gradient("", heatscale[1], heatscale[2], bias = 10) 

它工作正常,問題是,在右側的顏色圖例太小。有沒有辦法讓顏色圖例變大並增加圖例的字體大小?

感謝,

KZ

回答

8

我們沒有你melt.m數據,所以你給的代碼是不可重複的。使用diamonds數據集與ggplot2來作爲一個例子,雖然:

ggplot(diamonds, aes(x=table, y=price)) + 
    geom_bin2d() + 
    scale_fill_gradient("", 'ghostwhite', 'steelblue', bias=10) + 
    opts(legend.key.width=unit(1, "in"), 
     legend.text = theme_text(size=30)) 

legend.key.widthlegend.text是你在找什麼。我用誇張的尺寸來使它更加明顯。

Result of ggplot command

有關可用選項的詳細信息,請參閱https://github.com/hadley/ggplot2/wiki/+opts%28%29-List

3

我嘗試這樣做,發現R或GGPLOT2已經在過去的四年中變化。它得到的錯誤:

Error: 'opts' is deprecated. Use 'theme' instead. (Defunct; last used in version 0.9.1) 

能得到它與代替了以下工作:

p + theme(legend.text = element_text(size=30),legend.key.size = unit(1, "in")) 

最初試圖只是改變文字大小,而與它改變密鑰大小或成爲不可讀。另外,unit需要明確加載的庫library(grid)