Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

I am having a 3 columns data frame in R that looks like this:

> head(data)

          V.hit    J.hit  frequency

1 IGHV1-62-3*00 IGHJ2*00 0.51937442

2   IGHV5-17*00 IGHJ3*00 0.18853542

3    IGHV3-5*00 IGHJ1*00 0.09777304

4    IGHV2-9*00 IGHJ3*00 0.03040866

5   IGHV5-12*00 IGHJ4*00 0.02900040

6   IGHV5-12*00 IGHJ2*00 0.00910554

On this data frame I want to design a Heat map so that the X-axis will be "V.hit" and the Y-axis will be "J.hit", and the values of the heatmap will be the frequency . I have implemented the following code:


newData <- with(data, interp(x = `V hit`, y = `J hit`, z = frequency))

but I'm getting this error:

Error in interp.old(x, y, z, xo, yo, ncp = 0, extrap = FALSE, duplicate = duplicate,  : 

  missing values and Infs not allowed

Note that I am interested of the freq for each combination of V+j

1 Answer

0 votes
by (108k points)

With the help of complete() function, you can create all possible pattern of V.hit and J.hit. Now if you are getting any NA values then simply with the help of complete(), just assign the NA values to 0. Then finally for plotting the graph, just use geom_tile() function. Refer to the code below:


complete(foo, V.hit, nesting(J.hit), fill = list(frequency = 0)) %>% 

ggplot(aes(x = J.hit, y = V.hit, fill = frequency)) +


imageIf you want to know more about r programming then do check out the link. 

Browse Categories