Home > Blockchain >  How to create this chart in R or ggplot
How to create this chart in R or ggplot

Time:02-11

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

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

CodePudding user response:

What you need is geom_raster.

library(tidyverse)

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

geom_raster

CodePudding user response:

Try a heatmap with ggplot2 using geom_tile()

ggplot(df, aes(A, B, fill= Cat))   
  geom_tile()  
  scale_fill_manual(values = c("blue", "green", "red"))  
  geom_text(aes(label = Cat), size=5)

enter image description here

  • Related