如何用以平均值着色的较小矩形填充矩形

时间:2019-02-24 12:50:41

标签: r ggplot2

我有如下数据:

data_ex <- data.frame(x = runif(1000, 0, 10),
                      y = runif(1000, 0, 10),
                      z = runif(1000, 0, 1))

因此,基本上,这是正方形(但也可以是矩形)内的点(x, y),其值为z。我想将此平面划分为100个较小的正方形(矩形),并在其中取平均值z。所以我做了以下事情:

data_ex <- data_ex %>% 
  mutate(x2 = cut(x, breaks = 0:10), 
         y2 = cut(y, breaks = 0:10)) %>%
  group_by(x2, y2) %>%
  mutate(z = mean(z)) %>% 
  ungroup()

现在我要绘制它,并使用平均z值作为每个小方块(矩形)的颜色。可能地,我可以使用geom_tile来进行此操作(如下所示),但是它需要图块的中心作为输入。

data_ex %>% 
  ggplot() +
  geom_rect(aes(xmin = 0, xmax = 10, ymin = 0, ymax = 10), fill = 'white') +
  geom_tile(aes(x_center, y_center, fill = z))

我可能可以将其提取为x2y2的中心,但这似乎有点麻烦。因此,我想知道是否有更快的方法来执行适当的计算或以其他方式制作所需的图。

3 个答案:

答案 0 :(得分:2)

您可以使用 @Override public void render(float delta) { Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); game.batch.setProjectionMatrix(camera.combined); game.batch.begin(); ... floor函数创建任意矩形大小,然后计算这些间隔的中点。我对您的第二个代码块做了一些修改:

ceiling

然后绘制data_ex <- data_ex %>% mutate(x2 = cut(x, breaks = 0:10), y2 = cut(y, breaks = 0:10)) %>% group_by(x2, y2) %>% mutate(mean_z = mean(z), x_mid = floor(x) + (ceiling(x) - floor(x))/2, y_mid = floor(y) + (ceiling(y) - floor(y))/2, height = ceiling(y) - floor(y), width = ceiling(x) - floor(x)) %>% ungroup() 并为aes()指定高度和宽度参数:

geom_tile()

如果您将data_ex %>% ggplot() + geom_rect(aes(xmin = 0, xmax = 10, ymin = 0, ymax = 10), fill = 'white') + geom_tile(aes(x = x_mid, y = y_mid,height = height, width = width, fill = mean_z)) 应用于cutx且有不同的间隔,这也适用于非正方形矩形。

y

答案 1 :(得分:2)

编辑:OP要求一种使装箱工作适用于任意规模和装箱尺寸的方法。

可使用以下功能使装箱更灵活:

library(tidyverse)
bin_df <- function(df, x_binwidth, y_binwidth) {
  df %>%
    mutate(x2 = x_binwidth * (floor(x/x_binwidth) + 0.5), 
           y2 = y_binwidth * (floor(y/y_binwidth) + 0.5)) %>%
    group_by(x2, y2) %>%
    summarize(z = mean(z)) %>% 
    ungroup()
}

data_ex %>%
  bin_df(x_binwidth = 1, y_binwidth = 1) %>%
  ggplot() +
  geom_tile(aes(x2, y2, fill = z)) +
  scale_x_continuous(breaks = 0:10)

enter image description here

data_ex %>%
  bin_df(x_binwidth = 2, y_binwidth = 2) %>%
  ggplot() +
  geom_tile(aes(x2, y2, fill = z)) +
  scale_x_continuous(breaks = 0:10)

enter image description here

答案 2 :(得分:0)

并非所有事物都应该是ggplot2,所以我将基于spraster软件包添加一个替代解决方案。

代码如下:

library(sp)
library(raster)

set.seed(2222)

# Lets create 10 x 15 tiles
NCOLS = 10
NROWS = 15

data_ex <- data.frame(x = runif(1000, 0, 10),
                      y = runif(1000, 0, 10),
                      z = runif(1000, 0, 1))

# Create spatial points
dat_sp <- SpatialPointsDataFrame(data_ex[, 1:2], data = data_ex["z"])

# Create reference raster
r <- raster(ncols = NCOLS, nrows = NROWS, ext = extent(c(0, 10, 0, 10))) 

# Convert to a raster with z averaging
# Also could be any aggregation function like min, max, etc.
dat_rast <- rasterize(dat_sp, r, field = "z", fun = mean)

# Plot with base graphics
plot(dat_rast)

结果如下: Result with base graphics

如果您仍然想使用ggplot进行绘制,则可以使用graphVis包:

# Plot with ggplot2
library(ggplot2)
library(rasterVis)

gplot(dat_rast) + geom_tile(aes(fill = value))

结果: enter image description here