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.
The code that produced it is below.
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()
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()
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!