geom_tile图的可变大小 [英] Variable Size of geom_tile plot

查看:200
本文介绍了geom_tile图的可变大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,该代码接收位置数据以及该位置数据的值,然后使用geom_tile对其进行绘制.图的矩阵大小在数据项与数据项之间不是恒定的,并且几何图形块中的每个单元"都有可能包含另一个矩阵,该矩阵的大小也是不一致的.只要附加的细节单元格为2x2,我当前即可使用的代码,但是由于尝试适应其他任何大小(例如5x5)而失败.该代码要求用户输入主要的x和y距离(例如,x = c(0,2,4,6,8)的主要距离为2)以及附加详细信息单元的大小.下图是2x2附加详细信息单元的成功geom_tile.

I have a code that takes in positional data as well as values at that positional data and then plots it with geom_tile. The matrix size of the plot is not constant from data item to data item and each "cell" in the geom tile has a possibility of containing an additional matrix, again of inconsistent size. The code that I have currently works as long as the additional detail cell is a 2x2, but is failing with an attempt at adaptation to any other size, say a 5x5. The code requires the user to input the major x and y distances (x=c(0,2,4,6,8) has a major distance of 2 for example) as well as the size of the additional detail cell. An image below is the successful geom_tile for a 2x2 additional detail cell.

产生它的代码在下面.

  x <- c(0,0,4,3,3,5,5)
  y <- c(0,4,0,3,5,3,5)


  #USER INPUT
  major_x_dist <- 4 #x distance between the major data points 
  major_y_dist <- 4 #x distance between the major data points 
  division_cells <- as.character("2x2") #size of the cell containing additional detail



  #######################################
  if (division_cells == "2x2") {
    div_cells <- 2
  } else if (division_cells == "3x3") 
  { div_cells <- 3
  } else if (division_cells == "4x4")
  { div_cells <- 4
  } else if (division_cells == "5x5")
  { div_cells <- 5
  } else 
  { div_cells <-1
  }
  data_width <- ifelse(x%% major_x_dist==0, major_x_dist, major_x_dist/div_cells)
  data_height <- ifelse(y%% major_y_dist==0, major_y_dist, major_y_dist/div_cells)


  data_val <- sample(0:100, 7)
  alldata <-data.frame(x, y, data_width, data_height, data_val)




  ggplot(data= alldata, aes(x=x, y=y, width=data_width, height=data_height)) +
    geom_tile(fill = "white", color="black") + 
    geom_text(aes(label = data_val), colour="black") +
    coord_fixed()

下面是对5x5附加单元格的尝试改编.

The attempted adaptation for a 5x5 additional cell is below.

  x <- c(0,0,0,2,2,2,4,4,4,-0.8,-0.8,-0.8,-0.8,-0.8,-0.4,-0.4,-0.4,-0.4,-0.4,0,0,0,0,0.4,0.4,0.4,0.4,0.4,0.8,0.8,0.8,0.8,0.8)
  y <- c(0,2,4,0,2,4,0,2,4,3.2,3.6,4,4.4,4.8,3.2,3.6,4,4.4,4.8,3.2,3.6,4.4,4.8,3.2,3.6,4,4.4,4.8,3.2,3.6,4,4.4,4.8)


  #USER INPUT
  major_x_dist <- 2 #x distance between the major data points 
  major_y_dist <- 2 #x distance between the major data points 
  division_cells <- as.character("5x5") #size of the cell containing additional detail 



  #######################################
  if (division_cells == "2x2") {
    div_cells <- 2
  } else if (division_cells == "3x3") 
  { div_cells <- 3
  } else if (division_cells == "4x4")
  { div_cells <- 4
  } else if (division_cells == "5x5")
  { div_cells <- 5
  } else 
  { div_cells <-1
  }
  data_width <- ifelse(x%% major_x_dist==0, major_x_dist, major_x_dist/div_cells)
  data_height <- ifelse(y%% major_y_dist==0, major_y_dist, major_y_dist/div_cells)


  data_val <- sample(0:100, 33)
  alldata <-data.frame(x, y, data_width, data_height, data_val)




  ggplot(data= alldata, aes(x=x, y=y, width=data_width, height=data_height)) +
    geom_tile(fill = "white", color="black") + 
    geom_text(aes(label = data_val), colour="black") +
    coord_fixed()

请注意,整个矩阵的大小,数据点之间的主要距离,附加详细信息单元的位置以及附加详细信息单元的大小均与使用2x2附加详细信息单元的解决方案不同.看起来文本在正确的位置,但是单元格不在正确的位置.我认为问题可能与附加明细单元的中心数据点位于主要点(0,4)上有关.此代码产生的图如下.

Note that the size of the overall matrix, major distances between data points, location of the additional detail cell, and size of the additional detail cell are all different from the solution that works with a 2x2 additional detail cell. It appears that the text is in the correct location, but the cells are not. I think the issue might have to do with the fact that the center data point of the additional detail cell lies on a major point (0,4). The plot that this code produces is below.

可以提供任何故障排除建议,深表感谢!

Any troubleshooting advice that can be provided is much appreciated!

推荐答案

我认为您没有识别小方块的方法.不太清楚为什么,但是我认为回到这一点可能会更容易.这是一个广义的解决方案.首先,我将建立一些数据-随机选择所有尺寸,正方形的数目和子网格的位置...

I don't think the method you have of identifying the small squares works. Not quite sure why, but I thought it might be easier to go back to scratch on this. Here is a generalised solution. First I will set up some data - with the dimensions, number of squares and location of the sub-grid all picked at random...

large_x <- sample(2:5,1) #large grid no of x squares
large_y <- sample(2:5,1) #large grid no of y squares
small_x <- sample(2:5,1) #small grid no of x squares
small_y <- sample(2:5,1) #small grid no of y squares
large_w <- round(runif(1,0.5,1.5),2) #width of large squares
large_h <- round(runif(1,0.5,1.5),2) #height of large squares
df <- expand.grid(x=large_w*(1:large_x),y=large_h*(1:large_y)) #large grid
divsq <- sample(nrow(df),1) #random row of df to determine square to divide
sm_x <- df$x[divsq] #coordinates of divided square
sm_y <- df$y[divsq]
df <- rbind(df[-divsq,], #large grid without subdivided square
            expand.grid(x=sm_x-large_w*((1+1/small_x)/2-(1:small_x)/small_x), #x coordinates of small grid
                        y=sm_y-large_h*((1+1/small_y)/2-(1:small_y)/small_y))) #y coordinates of small grid
df$val <- sample(0:100,nrow(df))
df <- df[sample(nrow(df)),] #shuffle df for good measure!

现在,我将忽略所有随机参数,而只使用df,它仅包含xyval列.该方法是查看常数y的x值之间的间隔(反之亦然),并使用此间隔来计算出小的平方特性.然后,可以使用此信息来标记每个数据点是否属于一个小正方形,然后其余的就很简单了.

Now I am going to ignore all the random parameters and just work with df, which only contains x, y and val columns. The approach is to look at the intervals between x values for constant y (and vice versa) and use this to work out the small square characteristics. This information can then be used to mark whether each data point belongs to a small square, after which the rest is straightforward.

xdists <- tapply(df$x,df$y,function(z) diff(sort(z))) #list of differences between x values for constant y
ydists <- tapply(df$y,df$x,function(z) diff(sort(z))) #list of differences between y values for constant x
smallw <- min(unique(unlist(xdists))) #identify small width
smallh <- min(unique(unlist(ydists))) #identify small height

#the next lines check for rows that contain diffs equal to the small values, and return the appropriate values of x or y 
smally <- as.numeric(names(xdists)[sapply(xdists,function(z) min(abs(z-smallw))<0.0000001)]) #values of y corresponding to small grid
smallx <- as.numeric(names(ydists)[sapply(ydists,function(z) min(abs(z-smallh))<0.0000001)]) #values of x corresponding to small grid

nx <- length(smallx) #x-size of small grid
ny <- length(smally) #y-size of small grid

#this checks which data points are in small squares (allowing some tolerance for rounding)
df$small <- mapply(function(x,y) (min(abs(x-smallx))<0.0000001 & 
                                  min(abs(y-smally))<0.0000001),df$x,df$y)

df$w <- ifelse(df$small,smallw,smallw*nx)
df$h <- ifelse(df$small,smallh,smallh*ny)

ggplot(data=df, aes(x=x, y=y, width=w, height=h)) +
  geom_tile(fill = "white", color="black") + 
  geom_text(aes(label = val), colour="black") +
  coord_fixed()

这篇关于geom_tile图的可变大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆