Jasmine Koh
Jasmine Koh

Reputation: 41

How can I create a heat map in R with column elements as x and y axis?

I'm trying to create a heat map with data that looks like the following.

                     baseline.skill        baseline.job_profile baseline.skillpostingpct
45                          Chinese Analyst II Product Security              0.001718213
46                        Energetic Analyst II Product Security              0.001718213
47                       Persuasion Analyst II Product Security              0.001718213
48               Physical Abilities Analyst II Product Security              0.013745704
49                 Telephone Skills Analyst II Product Security              0.001718213
50             Communication Skills       Application Architect              0.348378894
51         Teamwork / Collaboration       Application Architect              0.272449142
52                         Planning       Application Architect              0.194294342
53                  Problem Solving       Application Architect              0.191075969
54                         Research       Application Architect              0.133343929
55                          Writing       Application Architect              0.128536236
56                  Troubleshooting       Application Architect              0.135012715
57                       Leadership       Application Architect              0.103623649
58                       Creativity       Application Architect              0.104140178
59                        Mentoring       Application Architect              0.087492053
60            Written Communication       Application Architect              0.085902734
61              Presentation Skills       Application Architect              0.079148125
62                  Detail-Oriented       Application Architect              0.053520343
63      Verbal / Oral Communication       Application Architect              0.054036872
64 Building Effective Relationships       Application Architect              0.036594088
65            Organizational Skills       Application Architect              0.039494596

I've tried different ways to create the heat map and haven't been able to find something that works for the way I want it to. I'm planning to have one axis have all the different skills shared across different roles and the other axis to be the list of different job profiles. I was thinking the heat map could be filled with four colors (for skillpostingpct), broken up evenly from 0-100%.

This is also just an excerpt of the data, I believe there are around 20 different job profiles that would go on the heat map and around 40+ skills but I can't figure out how to get something to work for what I have.

I'd greatly appreciate any help, thank you!

Upvotes: 0

Views: 437

Answers (1)

AndrewGB
AndrewGB

Reputation: 16876

You can use geom_tile from ggplot2. Here, I use quantile to set the breaks, but these could be set another way depending on your needs.

library(tidyverse)

ggplot(df, aes(factor(`baseline.job_profile`), `baseline.skill`, fill= `baseline.skillpostingpct`)) +
  geom_tile() +
  scale_fill_viridis_c(breaks = as.numeric(quantile(df$baseline.skillpostingpct, probs = seq(0, 1, 0.25))))

Output

enter image description here

Data

df <- structure(list(baseline.skill = c("Chinese", "Energetic", "Persuasion", 
"Physical Abilities", "Telephone Skills", "Communication Skills", 
"Teamwork / Collaboration", "Planning", "Problem Solving", "Research", 
"Writing", "Troubleshooting", "Leadership", "Creativity", "Mentoring", 
"Written Communication", "Presentation Skills", "Detail-Oriented", 
"Verbal / Oral Communication", "Building Effective Relationships", 
"Organizational Skills"), baseline.job_profile = c("Analyst II Product Security", 
"Analyst II Product Security", "Analyst II Product Security", 
"Analyst II Product Security", "Analyst II Product Security", 
"Application Architect", "Application Architect", "Application Architect", 
"Application Architect", "Application Architect", "Application Architect", 
"Application Architect", "Application Architect", "Application Architect", 
"Application Architect", "Application Architect", "Application Architect", 
"Application Architect", "Application Architect", "Application Architect", 
"Application Architect"), baseline.skillpostingpct = c(0.001718213, 
0.001718213, 0.001718213, 0.013745704, 0.001718213, 0.348378894, 
0.272449142, 0.194294342, 0.191075969, 0.133343929, 0.128536236, 
0.135012715, 0.103623649, 0.104140178, 0.087492053, 0.085902734, 
0.079148125, 0.053520343, 0.054036872, 0.036594088, 0.039494596
)), class = "data.frame", row.names = c(NA, -21L))

Upvotes: 1

Related Questions