2

I have a 4x4 geom_tile plot where the upper right cell contains an additional 4x4 cell. I am wanting the plot to appear as it does below.

Desired output visuals

The positions are listed inside the cells for clarity, however, the code makes the data value corresponding with the position placed in the cell. The sample code I provided currently produces this:

Current ggplot output

I am not able to manipulate the data values in terms of position (values for x and y cannot be changed) and the cell containing additional breakdown will not always be in the upper right so a solution that does not hardcode this location in would be appreciated.

If this is possible I would greatly appreciate any assistance you can provide!

x <- c(0,0,1,0.5,0.5,1,1)
y <- c(0,1,0,0.5,1,0.5,1)
data_val <- sample(0:100, 7)
alldata <-data.frame(x, y, data_val)


    ggplot(data= alldata, aes(x, y)) +
    geom_tile(colour = "black") + 
    geom_text(aes(label = data_val),colour="white")
User247365
  • 665
  • 2
  • 11
  • 27

1 Answers1

3

This requires a slight change to your notation, so that x and y correctly locate the centre of each square, and an additional width parameter (which with this system really only depends on whether x and y are even or odd). For other quartered squares, use -1/+1 instead of 3/5 as appropriate.

x <- c(0,0,4,3,3,5,5)
y <- c(0,4,0,3,5,3,5)
w <- ifelse(x%%2==0, 4, 2)
data_val <- sample(0:100, 7)
alldata <- data.frame(x, y, w, data_val)

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

enter image description here

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32
  • Andrew, could you please further explain the line `w <- ifelse(x%%2==0, 4, 2)` so that I can adapt it in different situations? I have data that has different x and y positions, different dimensions (3x3, 4x4 and 5x5 instead of the 2x2 that I gave as a sample) etc. and I would like to understand the logic of that line so that I can modify it to accommodate a wide variety of inputs. – User247365 May 04 '17 at 20:04
  • Yes - w (the width of a square) is either 2 or 4. The big squares are centred on even numbers (`x%%2==0`) and the small squares are centred on odd numbers. It should work for larger grids as long as you use this coordinate system. – Andrew Gustar May 04 '17 at 20:17
  • The way that the data is collected, there is not a guarantee that the numbers will fall on "pretty" numbers. For example, I have a data set that is a 5x5 with `x = -0.84, -0.42, 0, 0.42, 0.84` and y=`-0.71, -0.355, 0, 0.355, 0.71`. There is then an additional 5x5 matrix centered around the point (0.84, 0.71).How would I structure the line of code defining width so that it is able to handle a wide variety of positional input like that? I realize that is a pretty in depth question for a comment on a post so I can ask a new question if additional detail is required to convey my problem. – User247365 May 04 '17 at 22:55
  • My trick won't work if the smaller grid is 5x5, or if your `x` and `y` intervals are different. In that case you will also need a separate `height` column and will need to find a way of setting `width` and `height` equal to the large x or y interval for the main squares, and those values over 5 (or whatever) for the small ones. – Andrew Gustar May 05 '17 at 06:32
  • If you know which are the large and small squares, then separate them, sort by `x` (or `y`), and then `max(diff(x))` will give the appropriate width to use. If you don't know which are the small squares it is more tricky, but `sort` and `diff` will get you some of the way! – Andrew Gustar May 05 '17 at 06:52
  • I have been working with what you provided throughout the day and have a few more problems that I have run in to, the first of which is how to adapt your solution to cells that have other than a 2x2 for an additional detail cell. I have attempted a solution that does not break having a 2x2, but also does not allow for a 5x5 to be used. I am posting a new question that I would greatly appreciate your input on. http://stackoverflow.com/questions/43814267/variable-size-of-geom-tile-plot – User247365 May 05 '17 at 21:55