gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:47:36 30 4
gpt4 key购买 nike

我有以下数据框:

 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(expand = c(0, 0)) 不适用于 y 轴,但 scale_x_discrete(expand = c(0, 0)) 将适用于 x 轴以填充绘图空间。

最佳答案

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

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

## 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()

关于r - 如何使用 geom_raster 或 geom_tile 填充绘图区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10502273/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com