首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用geom_raster或geom_tile填充绘图区域

如何使用geom_raster或geom_tile填充绘图区域
EN

Stack Overflow用户
提问于 2012-05-09 00:07:36
回答 1查看 13.2K关注 0票数 5

我有以下数据框:

代码语言:javascript
复制
 id  variable   value
 ID1    1A      91.98473282
 ID1    2A      72.51908397
 ID1    2B      62.21374046
 ID1    2D      69.08396947
 ID1    2F      83.39694656
 ID1    2G      41.60305344
 ID1    2H      63.74045802
 ID1    9A      58.40839695
 ID1    9C      61.10687023
 ID1    9D      50.76335878
 ID1    9K      58.46183206

我正在使用ggplot2生成包含数据的热图:

代码语言:javascript
复制
     ggplot(data, aes(variable, id)) +
              geom_raster(aes(fill = value)) + 
              scale_fill_gradient(low = "white",
              high = "steelblue")

图看起来像这样:http://dl.dropbox.com/u/26998371/plot.pdf

我希望瓦片填充y轴上的绘图空间,而不是在上面和下面留下一个空间。

我相信有一个简单的答案。任何帮助都将不胜感激。

扩展(scale_y_discrete= c(0,0))不适用于y轴,但扩展(scale_x_discrete= c(0,0))将适用于x轴以填充绘图空间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-09 04:41:46

更新该问题似乎已在最新版本的ggplot2中得到解决。

这与id因子中只有一个级别有关。将id因子更改为数字,或更改id因子,使其具有两个级别,然后平铺填充空间。此外,使用原始id因子的coord_equal()将给出一个很长很窄的图,但会再次填充空间。

代码语言:javascript
复制
## Your data
df = read.table(text = "
id  variable   value
ID1    1A      91.98473282
ID1    2A      72.51908397
ID1    2B      62.21374046
ID1    2D      69.08396947
ID1    2F      83.39694656
ID1    2G      41.60305344
ID1    2H      63.74045802
ID1    9A      58.40839695
ID1    9C      61.10687023
ID1    9D      50.76335878
ID1    9K      58.46183206", header = TRUE, sep = "")

library(ggplot2)

 # Change the id factor
 df$id2 = 1                   # numeric
 df$id3 = c(rep("ID1", 5), rep("ID2", 6))      # more than one level

 # Using the numeric version
 ggplot(df, aes(variable, id2)) +
          geom_raster(aes(fill = value)) + 
          scale_y_continuous(breaks = 1, labels = "ID1", expand = c(0,0)) + 
          scale_x_discrete(expand = c(0,0)) +
          scale_fill_gradient(low = "white",
          high = "steelblue")

代码语言:javascript
复制
# Two levels in the ID factor
ggplot(df, aes(variable, id3)) +
          geom_tile(aes(fill = value)) + 
          scale_fill_gradient(low = "white",
          high = "steelblue") 

# Using coord_equal() with the original id variable
ggplot(df, aes(variable, id)) +
          geom_tile(aes(fill = value)) + 
          scale_fill_gradient(low = "white",
          high = "steelblue") +
          coord_equal()
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10502273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档