anup
anup

Reputation: 475

Color in ggplot, continuous value supplied to discrete scale

I am plotting points over a heat map produced in ggplot2. delta is a data frame containing points to be plotted over heat map. The variable plt stores the ggplot image.

The heat map is produced by the code from this link (could not post here because of text limitation). Also, the reproducible code for all required data frames is in the link.

https://justpaste.it/65iu7

Now to superimpose points over the heat map, I used the code below:

plt0 <- plt + geom_point(data = delta, aes(x = dP/100, y = dT, z = NULL,  color = rcp, shape = future))
plt0

It gives error:

Error: Continuous value supplied to discrete scale.

If I remove color = future from the above code, it works. But I need to have color coded points as this code produces:

ggplot()+geom_point(data = delta, aes(x = dP/100, y = dT, z = NULL, color = rcp, shape = future))

What is producing this error and how can I solve it?

Upvotes: 1

Views: 2065

Answers (2)

rar
rar

Reputation: 924

I did not change the code except for the first line. Instead of :

plt<-ggplot(new.data, aes(x = hh/100, y = tt, z = floor(W))) + geom_tile(aes(fill = W)) + ...

Used

plt<-ggplot()+ geom_tile(new.data, aes(x = hh/100, y = tt, fill = W)) + ...

This is to ascertain that we call empty ggplot and then add geom_tile with new.data and since the ggplot has not been assigned any data as default, we can later on add delta for geom_point. The output is as follows: enter image description here

Upvotes: 0

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

Is it ok like this?

plt <- ggplot() + geom_tile(data=new.data, aes(x = hh/100, y = tt, fill=W)) +
  geom_contour(data=new.data, bins = 10, 
               aes(x = hh/100, y = tt, #color = ..level.., 
                   z = floor(W)), 
               show.legend = FALSE) +
  ylab("Change in temperature in degree Celsius") +
  xlab("percentage change in precipitation") +
  scale_fill_gradientn(name = "W (in m3/year)",
                       values = scales::rescale(quantile(new.data$W)),
                       limits = c(min(new.data$W),max(new.data$W)),
                       breaks = seq(round(min(new.data$W)/1000000)*1000000,
                                    round(max(new.data$W)/1000000)*1000000,
                                    (round(max(new.data$W)/1000000)*1000000-round(min(new.data$W)/1000000)*1000000)/3),
                       colors = rainbow(7), guide = "colorbar") +
  scale_x_continuous(breaks = seq(-0.3,0.3, 0.1), label = scales::percent) +
  scale_y_continuous(breaks = seq(-1, 6, 1)) +
  ggtitle("Variation of average annual sediment production with \n temperature and precipitation")+
  guides(fill = guide_colorbar(barwidth = 0.5, barheight = 10))

plt

plt + 
  geom_point(data = delta, aes(x = dP/100, y = dT,
                               color = rcp, shape = future))

enter image description here

Upvotes: 1

Related Questions