简体   繁体   English

ggplot2中的scale_colour_gradient2不能准确显示colors的low还是high?

[英]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")

代码打印

The minimum value of qvalue is not displayed in red. qvalue的最小值不显示为红色。 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?如何让图中的最小值显示为红色,最大值显示为蓝色,0.01显示为黄色?

Thanks!谢谢!

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)reprex package (v0.3.0) 创建于 2020-05-27

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).一个小提示,假设您的qvalue变量是某种校正后的 p 值,这些通常更直观地表示为 log10 标度(或 -log10 标度)。 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)
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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