0

I have the following df:

     frequency  name sample_type exist
 1:    0.0115   sam           1     1
 2:    0.3000   sam           2     0
 3:    0.0000   sam           3     0
 4:    0.0141  bill           1     1
 5:    0.0124  bill           3     1
 6:    0.0000  bill           2     0
 7:    0.0136  mike           3     1
 8:    0.0000  mike           1     0
 9:    0.0000  mike           2     0
10:    0.0112   leo           1     1
11:    0.0000   leo           2     0
12:    1.0000   leo           3     0
13:    0.0108  brad           3     1
14:    0.0000  brad           2     0
15:    0.9900  brad           1     0
16:    0.0286 randy           3     1
17:    0.0000 randy           2     0
18:    0.7000 randy           1     0

I want to create tile plot/heatmap plot, so I wrote the script:

library(data.table)  
ra_df <- fread("G:\\My Drive\\Net2\\test_1.csv",
           select=c(1,2,3,4))

library(ggplot2)

ra_figure <- ggplot(ra_df, aes(name, factor(sample_type), fill = frequency)) + 
geom_tile(colour="white",size=0.25) +
scale_fill_gradientn(colours = c("white", "lightgreen"), values = c(0,1)) +
theme(axis.text.x = element_text(angle = 90, hjust=1), 
    legend.position="none", axis.title.x = element_blank(),axis.title.y = element_blank(),
    plot.title = element_text(hjust = 0.5,family = "Helvetica", face = "bold.italic", size = (15),
                              colour="steelblue4"))+
scale_x_discrete(position = "top") + 
scale_y_discrete(labels=c("3"="c","2"="b", "1"="a"),expand=c(0,0))+ 
coord_fixed(ratio = 3)

And I got the following figure: enter image description here

As you can see the fill values have big differences between them, so the low values seem white, like zero values. The lower values are important for me to see.

How can I create this figure so the lower values will be visible as well?

Bella
  • 937
  • 1
  • 13
  • 25

1 Answers1

2

You can tweak scale_fill_gradientn as:

scale_fill_gradientn(colours = c("lightgreen", "green"), values = c(0,1))

where the first color (lightgreen) corresponds to your lowest value (0) and the second color (green) corresponds to your highest value (1).

Following up your comment:

scale_fill_gradientn(colours = c("white", "lightgreen", "green"), values = c(0, 0.0001, 1))

This should be fine unless you have values of ]0, 0,0001[. If so, just make it 0.000001 and so on.

OzanStats
  • 2,756
  • 1
  • 13
  • 26
  • Tried it, but it colors also the zero values in lightgreen. Is there a way to define that the zero values will be colored in white? – Bella Jul 30 '18 at 08:20
  • @Bello; I edited my answer accordingly. Please type `?scale_fill_gradientn` in console to learn about the parameters and other gradient functions in ggplot. – OzanStats Jul 30 '18 at 08:26
  • That worked, thanks a lot! – Bella Jul 30 '18 at 08:28