我有以下数据框:
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生成包含数据的热图:
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轴以填充绘图空间。
发布于 2012-05-09 04:41:46
更新该问题似乎已在最新版本的ggplot2中得到解决。
这与id因子中只有一个级别有关。将id因子更改为数字,或更改id因子,使其具有两个级别,然后平铺填充空间。此外,使用原始id因子的coord_equal()将给出一个很长很窄的图,但会再次填充空间。
## 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")

# 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()https://stackoverflow.com/questions/10502273
复制相似问题