2011-10-13 223 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)