How do you fix warning message: colourbar guide needs continuous scales?

Viewed 112

I would like to produce multiple contour plots using ggplot2 and

geom_contour_filled()

but the z values range is too large. To give you a little bit of an idea of what the values are, it ranges from -2,71 to -157,28. So I thought I should change the breaks so it covers all of these values.

The code below is not the data I work with, but it should represent the problem I have: The data

h_axis <- 10^(seq(log10(0.1), log10(1000),
                  length.out = 20))
a_axis <- 10^(seq(log10(0.1), log10(1000),
                  length.out = 20))
comb <- expand.grid(h_axis, a_axis)
h_val <- comb$Var2
a_val <- comb$Var1

values <- seq(-2, -150, length.out = 400)
dt <- data.frame(h = h_val, a = a_val, values)

First, let's say I don't change the breaks. Then, using this code

ggplot(dt, aes(x = log10(h_val), y = log10(a_val), z = values)) + 
  geom_contour_filled() + 
  #  geom_contour(color = "black", size = 0.1) + 
  xlab(expression(log[10](h))) + 
  ylab(expression(log[10](a))) + 
  guides(fill = guide_colorbar(title = expression('E ||'*g - hat(g)*'||'[2]*'')))

will produce the following figure:

enter image description here

So a lot of the area will be covered by the same colour, which is a problem since my data consists of multiple factors. Factor 1 is covered by the yellow, Factor 2 is covered by the green, and so on.

Then my second approach, is to add

bar <- 10^(seq(log10(-min(values)), log10(-max(values)),
               length.out = 100))

and put bar in the geom_contour_filled() like this

geom_contour_filled(breaks = -bar)

Then I get

enter image description here

which is nice! But, in both cases I get the following warning

Warning message:
colourbar guide needs continuous scales.

Also, the legend is not shown on the right side. What do I need to do to fix the warning and how can I make sure that the legend is shown?

1 Answers

Try guide_legend instead of guide_colorbar.

Related