Can I get this plot with gglplot?

Hi community, Im wanting to make a plot but dont know how start.

This is occupancy information.
Then I want to get a map that allows me to see the spaces occupied by ACC and the empty ones, in different color.

Maybe a waffle plot or only with ggplot is possible make this?

EX <- data.frame(LOTE=c('B1','B1','B1','B2','B2','C2','C2','INVEQ'),
                 CAMA=c(1,2,4,'6/11',7,6,11,5),
                 ACC=c(123,NA,12345,77,555,NA,112,55))

the desired graph looks something like this:

Tnks!

It might be easier to do that as a table, (the {gt} package is nice), but it is possible to do it with geom_tile(). In this example, I create a
background from a temp dataframe with expand_grid to draw the green tiles on:

EX <- data.frame(LOTE=c('B1','B1','B1','B2','B2','C2','C2','INVEQ'),
                 CAMA=c(1,2,4,6/11,7,6,11,5),
                 ACC=c(123,NA,12345,77,555,NA,112,55))

library(ggplot2)

ggplot(EX)+
  coord_fixed(clip='on')+
  geom_tile(data = expand_grid(x=1:12, y = as.factor(EX$LOTE)), 
            aes(x,y), fill="chocolate",color='black')+
  geom_tile(aes(y=LOTE, x=CAMA), fill="forestgreen",color='black', width = 1)+
  geom_text(aes(y=LOTE, x=CAMA, label=ACC),color='black',size=8/.pt)+
  scale_x_continuous(breaks = 1:12)

The first thing I'd do is transform your data so it's easier to work with:

EX <- data.frame(LOTE=c('B1','B1','B1','B2','B2','C2','C2','INVEQ'),
                 CAMA=c(1,2,4,'6/11',7,6,11,5),
                 ACC=c(123,NA,12345,77,555,NA,112,55))

library(tidyverse)

EX |> 
  separate_longer_delim(CAMA, '/')
#>    LOTE CAMA   ACC
#> 1    B1    1   123
#> 2    B1    2    NA
#> 3    B1    4 12345
#> 4    B2    6    77
#> 5    B2   11    77
#> 6    B2    7   555
#> 7    C2    6    NA
#> 8    C2   11   112
#> 9 INVEQ    5    55

Created on 2024-05-22 with reprex v2.0.2

Here's another possibility that builds on my previous comment and @mduvekot's suggestion (with full reprex at the bottom of post):

EX <- data.frame(LOTE=c('B1','B1','B1','B2','B2','C2','C2','INVEQ'),
                 CAMA=c(1,2,4,'6/11',7,6,11,5),
                 ACC=c(123,NA,12345,77,555,NA,112,55))

library(tidyverse)

# transform EX to longer table wth numeric values for CAMA and LOTE
EX.num <- 
  EX |> 
  # separate '6/11' into the numbers 6 and 7
  separate_longer_delim(CAMA, '/') |> 
  # convert LOTE to factor, ordered by decreasing row number
  mutate(LOTE = fct_reorder(LOTE, row_number(), .desc = T)) |> 
  # convert CAMA and LOTE values to numbers for easier plotting
  mutate(CAMA = CAMA |> as.numeric()) |> 
  mutate(LOTE.num = LOTE |> as.numeric()) |> 
  # remove rows that contain an NA
  drop_na()
EX.num |> 
  drop_na() |> 
  ggplot() +
  geom_tile(
    aes(CAMA, LOTE.num, width = 1, height = 1), 
    fill = 'forestgreen', 
    color = 'black'
    ) +
  geom_text(aes(CAMA, LOTE.num, label = ACC)) +
  scale_y_continuous(
    name = 'LOTE',
    limits = c(0.5, 4.5),
    breaks = 1:4,
    minor_breaks = 0.5:4.5,
    labels = EX.num |> pull(LOTE) |> levels(),
    expand = expansion()
  ) +
  scale_x_continuous(
    breaks = EX.num |> pull(CAMA) %>% {min(.):max(.)},
    expand = expansion()
) +
  theme(
    panel.background = element_rect(fill = 'chocolate'),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_line(color = 'black')
  )

Created on 2024-05-22 with reprex v2.0.2

Full reprex
EX <- data.frame(LOTE=c('B1','B1','B1','B2','B2','C2','C2','INVEQ'),
                 CAMA=c(1,2,4,'6/11',7,6,11,5),
                 ACC=c(123,NA,12345,77,555,NA,112,55))

library(tidyverse)

# transform EX to longer table wth numeric values for CAMA and LOTE
EX.num <- 
  EX |> 
  # separate '6/11' into the numbers 6 and 7
  separate_longer_delim(CAMA, '/') |> 
  # convert LOTE to factor, ordered by decreasing row number
  mutate(LOTE = fct_reorder(LOTE, row_number(), .desc = T)) |> 
  # convert CAMA and LOTE values to numbers for easier plotting
  mutate(CAMA = CAMA |> as.numeric()) |> 
  mutate(LOTE.num = LOTE |> as.numeric()) |> 
  # remove rows that contain an NA
  drop_na()

EX.num |> 
  drop_na() |> 
  ggplot() +
  geom_tile(
    aes(CAMA, LOTE.num, width = 1, height = 1), 
    fill = 'forestgreen', 
    color = 'black'
    ) +
  geom_text(aes(CAMA, LOTE.num, label = ACC)) +
  scale_y_continuous(
    name = 'LOTE',
    limits = c(0.5, 4.5),
    breaks = 1:4,
    minor_breaks = 0.5:4.5,
    labels = EX.num |> pull(LOTE) |> levels(),
    expand = expansion()
  ) +
  scale_x_continuous(
    breaks = EX.num |> pull(CAMA) %>% {min(.):max(.)},
    expand = expansion()
) +
  theme(
    panel.background = element_rect(fill = 'chocolate'),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_line(color = 'black')
  )

Created on 2024-05-22 with reprex v2.0.2

2 Likes

tnks for your help.
Was the solution.

1 Like

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.