2

Does anyone have any ideas on how to visualize something like this using R?

enter image description here

Basically I want to make a plot to track the percentage completeness of different jobs in a few projects. The percentage completeness should be color coded (in the example, dark blue means the job is 100% complete, white means 0% complete etc.) Here's some sample code to make the dataframe:

project <- c('Project 1', 'Project 2', 'Project 3', 'Project 4')

job1 <- c(100, 100, NA, 100)

job2 <- c(100, 100, NA, 100)

job3 <- c(100, NA, NA, NA)

job4 <- c(NA, 100, 100, 100)

job5 <- c(50, 100, 100, 100)

job6 <- c(0, 40, 100, 100)

df <- data.frame(project, job1, job2, job3, job4, job5, job6)

I've tried a gantt chart but couldn't get that to work as there is no time data. I've tried a bar chart but that didn't work as some of the projects have NA or 0 values followed by 100/50 etc. so the jobs don't line up. I also tried a heatmap but that doesn't really work either, as there needs to be a visible distinction between the NA values and the 0 values i.e. NA means that a job isn't applicable to that project, whereas 0 means the job hasn't started yet.

Any suggestions would be really appreciated!

Till
  • 3,845
  • 1
  • 11
  • 18
zxy
  • 23
  • 3

2 Answers2

2

DT (datatable) based solution:

library(DT)
options(DT.options = list(pageLength = 5))
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

# create 19 breaks and 20 rgb color values ranging from white to red
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
  {paste0("rgb(255,", ., ",", ., ")")}
datatable(df) %>% formatStyle(names(df), backgroundColor = styleInterval(brks, clrs))

Will give you a nice interactive html table looking like this enter image description here

Example from, and for more info, visit:

https://rstudio.github.io/DT/010-style.html

user12256545
  • 2,755
  • 4
  • 14
  • 28
1

First you have to change the data to a long format; tidyr::pivot_longer() does that. ggplot2::geom_tile() can then be used to create a heatmap plot.

library(tidyr)
library(ggplot2)
df |> 
  pivot_longer(-project) |> 
  ggplot(aes(name, project, fill = value)) +
  geom_tile()

You can tune it some more, to reach a result that more closely resembles your example:

library(tidyr)
library(ggplot2)
library(forcats)
library(dplyr)

df |> 
  pivot_longer(-project) |> 
  mutate(project = fct_rev(factor(project))) |> 
  ggplot(aes(name, project, fill = value)) +
  geom_tile(color = "grey", size = 3) +
  scale_x_discrete(position = "top") +
  labs(x ="", y = "")

Till
  • 3,845
  • 1
  • 11
  • 18