本文介绍了我如何强制ggplot的geom_tile填满每个方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用ggplot的geom_tile做一个因素面向的二维密度图。每个方面的比例尺都是从所有数据的最小值到所有数据的最大值,但每个方面的geom_tile只能扩展到 中绘制的数据范围。 b $ b 演示问题的示例代码: library(ggplot2) $ (数据帧(x = rnorm(500),y = rnorm(500)) data.limited 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) p> 运行代码,您将看到两个方面。一个方面显示了无限随机正态分布的密度图。第二个方面显示一个随机正常截断位于关于原点的2x2正方形内。 有限面中的geom_tile将被限制在这个小方框内,而不是填充面。 last_plot()+ scale_x_continuous(限制= c(-5,5))+ scale_y_continuous (限制= c(-5,5)) 最后三行绘制了具有指定x和y限制的相同数据,而我们请注意,在这种情况下,任何facet都不会将tile部分延伸到边缘。 是否有任何方法可以强制每个方面的geom_tile扩展到facet的全部范围?解决方案我认为您正在寻找 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)) 给出OP的说明,下面是一个通过手动设置面板背景的选项: 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()) 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: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)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. last_plot() + scale_x_continuous(limits=c(-5,5)) + scale_y_continuous(limits=c(-5,5))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 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))EDITGiven the OP's clarification, here's one option via simply setting the panel background manually: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()) 这篇关于我如何强制ggplot的geom_tile填满每个方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 05:03