使用ggplot2 geom_tile控制磁贴半径 [英] Controlling tile radius with ggplot2 geom_tile
问题描述
我试图用 ggplot2
创建一个热图来控制两个不同的连续变量。一个值只有大于0.对于这个变量,我想控制瓦片的半径,使得最大值是占用所有可用空间的瓦片,而0意味着瓦片的面积为0.到目前为止,使用 aes(size = ...)
没有给出任何结果,我感觉丢失了该怎么做。
示例:
library(dplyr)
library(ggplot2)
dat < -
data_frame(
factor_1 = as.factor(rep(c(1,2,3,4,5),5)),
factor_2 = as .factor(rep(c(1,2,3,4,5),each = 5)),
value_1 = rnorm(25),
value_2 = runif(25)
)
ggplot(dat)+
geom_tile(aes(x = factor_1,y = factor_2,fill = value_1,size = value_2))
产生
正如我们所看到的,有一个尺寸图例可以与 value_2
进行比例缩放,但这些图块大小一致。
我希望获得的结果与此链接中的前两个示例类似 corrplot函数,圆圈或正方形的大小可以变化,在它们周围留下一个白色边框。
宽度
和高度
美学: ggplot(dat)+
geom_tile(aes(x = factor_1,y = factor_2,fill = value_1,
width =值得注意的是,我得到了关于这些未知美学的警告,但他们显然正在设置瓷砖的宽度和高度。
I am trying to create a heatmap using ggplot2
that controls for two different continuous variables. One only has values greater than 0. For that variable, I would like to control the 'radius' of the tile such that the maximum value is a tile that takes up all available space, and 0 means that the tile will have an area of 0. So far, using aes(size = ...)
has not given any results, and I feel lost as to what to do.
Example:
library(dplyr)
library(ggplot2)
dat <-
data_frame(
factor_1 = as.factor(rep(c(1, 2, 3, 4, 5), 5)),
factor_2 = as.factor(rep(c(1, 2, 3, 4, 5), each = 5)),
value_1 = rnorm(25),
value_2 = runif(25)
)
ggplot(dat) +
geom_tile(aes(x = factor_1, y = factor_2, fill = value_1, size = value_2))
produces
As we can see, there is a size legend that scales with value_2
, but the tiles are all uniformly sized.
The result I would like to obtain is similar to the first two examples at this link for the corrplot function where the circles or squares can vary in size, leaving a white border around them.
解决方案 You can use the width
and height
aesthetics:
ggplot(dat) +
geom_tile(aes(x = factor_1, y = factor_2, fill = value_1,
width = value_2, height = value_2))
For some reason I get warnings about these being "unknown aesthetics", but they are obviously working to set the width and height of the tiles.
这篇关于使用ggplot2 geom_tile控制磁贴半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!