我如何强制ggplot的geom_tile填满每个方面? [英] How can I force ggplot's geom_tile to fill every facet?
问题描述
我正在使用ggplot的geom_tile做一个因素面向的二维密度图。每个方面的比例尺都是从所有数据的最小值到所有数据的最大值,但每个方面的geom_tile只能扩展到 演示问题的示例代码: p> 运行代码,您将看到两个方面。一个方面显示了无限随机正态分布的密度图。第二个方面显示一个随机正常截断位于关于原点的2x2正方形内。 有限面中的geom_tile将被限制在这个小方框内,而不是填充面。 最后三行绘制了具有指定x和y限制的相同数据,而我们请注意,在这种情况下,任何facet都不会将tile部分延伸到边缘。 是否有任何方法可以强制每个方面的geom_tile扩展到facet的全部范围? 我认为您正在寻找 < img src =https://i.stack.imgur.com/aWtMT.pngalt =编辑 给出OP的说明,下面是一个通过手动设置面板背景的选项: I am using ggplot's geom_tile to do 2-D density plots faceted by a factor. Every facet's scale goes from the minimum of all the data to the maximum of all the data, but the geom_tile in each facet only extends to the range of the data plotted in that facet. Example code that demonstrates the problem: Run the code, and you will see two facets. One facet shows a density plot of an "unlimited" random normal distribution. The second facet shows a random normal truncated to lie within a 2x2 square about the origin. The geom_tile in the "limited" facet will be confined inside this small box instead of filling the facet. These last three lines plot the same data with specified x and y limits, and we see that neither facet extends the tile sections to the edge in this case. Is there any way to force the geom_tile in each facet to extend to the full range of the facet? I think you're looking for a combination of EDIT Given the OP's clarification, here's one option via simply setting the panel background manually: 这篇关于我如何强制ggplot的geom_tile填满每个方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
library(ggplot2)
$ (数据帧(x = rnorm(500),y = rnorm(500))
data.limited < - 子集y = r norm(500)),x 1< 1& x> -1& y> -1)
mydata< -rbind(data.frame(groupvar =无限制,data.unlimited),
data.frame(groupvar =limited,data.limited))
ggplot(mydata)+
aes(x = x, y = y)+
stat_density2d(geom =tile,aes(fill = ..density ..),contour = FALSE)+
facet_wrap(〜groupvar)
last_plot()+
scale_x_continuous(限制= c(-5,5))+
scale_y_continuous (限制= c(-5,5))
scales =free
和 expand = c(0,0)
:
ggplot(mydata)+
aes(x = x,y = y)+
stat_density2d(geom =tile,aes(fill = ..density ..),contour = FALSE)+
facet_wrap(〜groupvar,scales =free)+
scale_x_continuous(expand = c(0,0))+
scale_y_continuous(expand = c(0,0))
ggplot(mydata)+
aes(x = x,y = y)+
stat_density2d(geom =tile,aes(fill = ..density ..),轮廓= FALSE)+
facet_wrap(〜groupvar)+
scale_fill_gradient(低=蓝,高=红)+
opts(panel.background = theme_rect(fill =blue ),panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank())
library(ggplot2)
data.unlimited <- data.frame(x=rnorm(500), y=rnorm(500))
data.limited <- subset(data.frame(x=rnorm(500), y=rnorm(500)), x<1 & y<1 & x>-1 & y>-1)
mydata <- rbind(data.frame(groupvar="unlimited", data.unlimited),
data.frame(groupvar="limited", data.limited))
ggplot(mydata) +
aes(x=x,y=y) +
stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
facet_wrap(~ groupvar)
last_plot() +
scale_x_continuous(limits=c(-5,5)) +
scale_y_continuous(limits=c(-5,5))
scales = "free"
and expand = c(0,0)
:ggplot(mydata) +
aes(x=x,y=y) +
stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
facet_wrap(~ groupvar,scales = "free") +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0))
ggplot(mydata) +
aes(x=x,y=y) +
stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
facet_wrap(~ groupvar) +
scale_fill_gradient(low = "blue", high = "red") +
opts(panel.background = theme_rect(fill = "blue"),panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank())