ryster
ryster

Reputation: 31

The scale_colour_gradient2 in ggplot2 cannot accurately display the colors of low or high?

ggplot(result) +
  geom_bar(aes(height=count,fill = qvalue)) +
  scale_fill_gradient2(midpoint = 0.01,
                       low = "red",
                       mid = "yellow",
                       high = "blue",
                       limits=c(min(result$qvalue),
                                max(result$qvalue))) +
  theme(axis.text.y = element_text(size = 12),
        axis.line.x = element_line()) +
  ylab("gene count")

code print

The minimum value of qvalue is not displayed in red. Why?
How can I make the minimum value in the figure be displayed in red, the maximum value displayed in blue, and 0.01 displayed in yellow?

Thanks!

Upvotes: 3

Views: 1589

Answers (1)

teunbrand
teunbrand

Reputation: 37933

The colour scale in your example gives the correct result, which is to make the colour gradient symmetric around the midpoint. However, that doesn't seem to be what you want. Here is how you could achieve what you describe:

library(ggplot2)
library(scales)

# Dummy data
df <- data.frame(
  cat = LETTERS[1:5],
  value = runif(5, 0, 10),
  qvalue = c(0.001, 0.01, 0.05, 0.1, 0.005)
)

ggplot(df, aes(cat, value)) +
  geom_col(aes(fill = qvalue)) +
  scale_fill_gradientn(
    colours = c("red", "yellow", "blue"),
    # The 0.01 has to fall at the correct spot in the [0,1] interval
    values = c(0, rescale(0.01, from = range(df$qvalue)), 1)
  )

Created on 2020-05-27 by the reprex package (v0.3.0)

A small note, assuming that you qvalue variable is some kind of corrected p-value, these are typically more intuitively represented on a log10 scale (or -log10 scale). Here is how you would do this:

ggplot(df, aes(cat, value)) +
  geom_col(aes(fill = log10(qvalue))) +
  scale_fill_gradientn(
    colours = c("red", "yellow", "blue"),
    values = c(0, rescale(log10(0.01), from = log10(range(df$qvalue))), 1)
  )

or

ggplot(df, aes(cat, value)) +
  geom_col(aes(fill = qvalue)) +
  scale_fill_gradientn(
    trans = "log10", # Transform at scale instead of in aes()
    colours = c("red", "yellow", "blue"),
    values = c(0, rescale(log10(0.01), from = log10(range(df$qvalue))), 1)
  )

Upvotes: 2

Related Questions