163
163

Reputation: 205

ggplot2 change colorbar label

I want to plot a matrix. In order to clearly see the low values, code is like below.

p <- ggplot(data = melt(x))
p + geom_tile(aes(x=Var2,y=Var1,fill = value))

Now in order to see details, I prefer use square root scale. But if I change fill = value to fill = sqrt(value) the colorbar range will change too(For example, original is c(0,100), now is c(0,10)).

What I want is to plot sqrt(value)but still use the colorbar of value

I checked guide_colorbar()but there is only argument about whether to show labels, no argument about how to set own labels.

Upvotes: 4

Views: 2677

Answers (1)

Joe
Joe

Reputation: 3991

Use the trans argument of of scale_fill_gradient:

p + 
    geom_tile(aes(x = Var2, y = Var1, fill = value)) + 
    scale_fill_gradient(trans = "sqrt")

Here's a sample plot, using the following data:

x <- matrix(1:16, 4, 4)

enter image description here

As you can see, trans transforms the data for both the plot and the colorbar, so your color scale will be root-transformed but you can identify colors to their original values.

Upvotes: 5

Related Questions