热图 ggplot2 颜色渐变 (scale_fill_gradient)

发布于 2024-12-09 17:32:18 字数 987 浏览 1 评论 0原文

我正在尝试使用 ggplot2 绘制热图,我想调整颜色条的大小并增加字体。

这是代码的相关部分:

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

I am trying to plot a heat map with ggplot2 and I would like to resize the colorbar and increase the font.

Here is the relevant part of the code:

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)

It works fine, the problem is that the color legend on the right side is too small. Is there a way to make the color legend bigger and increase the font size of the legend?

Thanks,

kz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长不大的小祸害 2024-12-16 17:32:18

我们没有您的 melt.m 数据,因此您提供的代码无法重现。但以 ggplot2 附带的 Diamonds 数据集为例:

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

We don't have your melt.m data, so the code you give is not reproducible. Using the diamonds dataset that comes with ggplot2 as an example, though:

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.width and legend.text are what you are looking for. I have used exaggerated sizes to make it more obvious.

Result of ggplot command

For more details on the options available, see https://github.com/hadley/ggplot2/wiki/+opts%28%29-List

近箐 2024-12-16 17:32:18

我尝试了一下,发现 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)显式加载的库

I tried this and found that R or ggplot2 have changed in the last four years. It yielded the error:

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

Was able to get it to work with the following instead:

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

Initially tried just changing the text size but had to change the key size with it or it becomes unreadable. Also, unit needs a library explicitly loaded with library(grid)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文