Create a figure with rectangule and arrow

I would like to create two similar rectangles next to the two I created. Insert the arrow too, as per the expected image I put below. However, these two new rectangles have no colors and no point P. For these new rectangles, the values are c(0, 1, 1, 1, 0) and c(1, 0, 0, 1, 0).

rectangle_width <- 1    
rectangle_height <- 0.2   
square_width <- 0.2   
square_height <- 0.2    

# Function to create a rectangle and add numbers inside
create_rectangle <- function(position_x, position_y, numbers, color) {
  # Create rectangle
  rect(position_x, position_y, position_x + rectangle_width, position_y + rectangle_height, border = "black", col = color)
  
  # Add numbers in the squares
  for (i in 1:length(numbers)) {
    square_position_x <- position_x + (i - 1) * square_width
    square_position_y <- position_y
    rect(square_position_x, square_position_y, square_position_x + square_width, square_position_y + square_height, border = "black")
    text(square_position_x + square_width/2, square_position_y + square_height/2, numbers[i], cex = 1.5, col = "black")
  }
}

# Create the plot
plot.new()

# First rectangle
numbers_rectangle1 <- c(0, 1, 0, 1, 0)
color_rectangle1 <- "lightblue"  
  create_rectangle(0, 0.4, numbers_rectangle1, color_rectangle1)
  
  # Second rectangle
  numbers_rectangle2 <- c(0, 1, 1, 1, 0)
  color_rectangle2 <- "white"  
    create_rectangle(0, 0, numbers_rectangle2, color_rectangle2)
    
    # Coordinates of the line and the "P" text
    line_x <- square_width * 2
    line_y <- 0.3 + square_height/2
    text_x <- square_width * 2 + square_width/2
    text_y <- 0.4 + square_height + 0.04
    
    # Add line and "P" text
    segments(line_x, line_y, line_x, line_y + square_height, lwd = 3)
    text(text_x, text_y, "P", cex = 1.5, col = "black", pos = 2)

Output:
Rplot01

Expected output:

I came up with a solution of ggplot2

library(tidyverse)

df <- bind_rows(
  expand_grid(x = 1:5,y = c(1,3)),
  expand_grid(x = 8:12,y = c(1,3))
) %>% arrange(y, x) %>% mutate(
  num = c(c(0,1,1,1,0),c(1,0,0,1,0),c(0,1,0,1,0),c(0, 1, 1, 1, 0)),
  fill_color = c(rep("A",5),rep("B",5),rep("B",5),rep("A",5))
)

ggplot(df) + 
  geom_rect(aes(xmin = x - .5, xmax = x + .5, ymin = y - .5, ymax = y + .5, fill = fill_color), color = "black") +
  geom_text(aes(x = x, y = y, label = num)) + 
  geom_segment(aes(x = 2.5, xend = 2.5, y = .3, yend = 3.7), arrow = arrow(length = unit(5, "pt"))) +
  geom_segment(aes(x = 9.5, xend = 9.5, y = .3, yend = 3.7), arrow = arrow(length = unit(5, "pt"))) +
  geom_segment(aes(x = 5.7, xend = 7.3, y = 2, yend = 2), 
               linewidth = 4,  lineend = "round",linejoin ='round', color = "black",
               arrow = arrow(length = unit(15, "pt"),type = "open")) +
  scale_fill_manual(values = c("gray90",  "skyblue")) + 
  ylim(0, 4)+
  xlim(0,13)+
  theme_void()+theme(panel.background = element_rect(fill = 'white'),
                     plot.margin = unit(c(0,0,0,0), "pt"),
                     legend.position = "none")

if you want to control the fill color of each cell, modify the fill_color column in the data. By default A refers to gray and B refers to blue.

2 Likes

Thanks @yifanliu, it's better this way!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.