Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create this chart in R or ggplot

Below is the code to create the data frame.

df <- data.frame (ID  = c('C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9','C10', 'C11', 'C12','C13', 'C14'),
                  A = c(1, 1, 2, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, 2), 
                  B = c(3, 3, 1, 1, 1, 1, 1, 2, 3, 3, 2, 2, 2, 2)
                  )

Code to create the Category column:

df$Cat <- ifelse(df$A == 2 & df$B == 2, 'MID',
                 ifelse(df$A == 2 & df$B == 1, 'GONE', 
                        ifelse(df$A == 1 & df$B == 1, 'GONE', 
                               ifelse(df$A == 1 & df$B == 2, 'GONE', 
                                      ifelse(df$A == 2 |df$A == 1 & df$B == 3, 'UP',
                               
                               'NO')))))

The chart I need to create:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I create the chart I want from the data above? I have tried using Treemaps for this but I don’t think that will work because it only allows comparing quantities by size in a fixed space. So what can be used? Any help will be greatly appreciate

>Solution :

What you need is geom_raster.

library(tidyverse)

df %>% ggplot(aes(B, A, fill = Cat)) + 
  geom_raster() +
  ylim(c(0.5, 3))

geom_raster

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading