简体   繁体   中英

heatplot ggplot2 error different behaviour between scale_fill_gradient2 and scale_fill_grey Error: Continuous value supplied to discrete scale

I made a heatplot for a presentation in color, and now I am trying to transform it to greyscale, the problem is that when I use exactly the same code replacing scale_fill_gradient2 by scale_fill_grey it does not work. I have seen similar posts, but all of the people were actually working with discrete variables, I am working with a continuous variable.

Here is an example dataset:

library(ggplot2)
DF <- data.frame(species = rep(c("a", "b", "c"), times= 3), variable = rep(c("Min","Max", "Mean"), each = 3), value = rnorm(9), sign = sample(c("+","-"), 9, replace = TRUE))

I plot this in full color and it works, here is my code for that:

p <- ggplot(DF, aes(x=variable, y=species)) +  geom_tile(aes(fill=value), colour = "black") + scale_fill_gradient2(low = "blue", high = 'red') +
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))+ geom_text(aes(label=sign), color = 'black')
p

Then I thought that if I changed scale_fill_gradient2 to scale_fill_grey it would change to greyscale, but it throws an error, here is that code

p2 <- ggplot(DF, aes(x=variable, y=species)) +  geom_tile(aes(fill=value), colour = "black") + scale_fill_grey(start = 0.2, end = 0.8) +
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))+ geom_text(aes(label=sign), color = 'black')
p2

The error I get is this one:

Error: Continuous value supplied to discrete scale

Can anyone tell me how to fix that?

Thanks

I found a way to do it:

p <- ggplot(DF, aes(x=species, y=variable)) +  geom_tile(aes(fill=value), colour = "black") + scale_fill_gradient2(low = "white", high = 'black') +
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5))+ geom_text(aes(label=sign), color = 'black')
p

That is not using the scale_fill_grey anyways, if someone else figures that out please post it too, that might give people more options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM