【问题标题】:New complexity to color coding based on percentile and another factor in ggplot基于百分位数的颜色编码的新复杂性和ggplot中的另一个因素
【发布时间】:2013-10-20 06:23:12
【问题描述】:

我想为下图中的颜色编码方案添加另一个复杂程度。我想说明绘制的每个值是否都通过了统计测试。因此,如果通过测试,这些点只会根据百分位数进行颜色编码,否则,我希望点是灰色的。

这是我的代码,因为我从第一篇帖子 Color code points based on percentile in ggplot 收到了所有有用的建议(注意:这是一些虚构的数据,尽管我有更多条目的真实数据:

dat <- data.frame(key = c("a1-a3", "a1-a2"), position = 1:100, fst = rlnorm(200, 0, 1), fet = rnorm(200, 0.24, 0.54))

#Get quantiles
quants <- quantile(dat$fst, c(0.95, 0.99))

dat$quant  <- with(dat, factor(ifelse(fst < quants[1], 0,
                                  ifelse(fst < quants[2], 1, 2))))

dat$fisher <- with(dat, factor(ifelse(fet > 1.30102999566398, 0, 1)))

dat$col <- with(dat, factor(ifelse(fet < 1.30102999566398, 3, quant)))

########theme set
theme_set(theme_bw(base_size = 10))

p1 <- ggplot(dat, aes(x=position, y=fst)) +
  geom_point(aes(colour = col, size=0.2)) +
  facet_wrap(~key, nrow = 1) +
  scale_colour_manual(values = c("black", "blue", "red", "grey"), labels = c("0-95", "95-99", "99-100", "fail")) +
  ylab(expression(F[ST])) +
  xlab("Genomic Position (Mb)") +
  scale_x_continuous(breaks=c(0, 1e+06, 2e+06, 3e+06, 4e+06), labels=c("0", "1", "2", "3", "4")) +
  scale_y_continuous(limits=c(0,1)) +
  theme(plot.background = element_blank(),
    panel.background = element_blank(),
    panel.border = element_blank(),
    legend.position="none",
    legend.title = element_blank()
    )

tiff(Fstvalues_colourcode3.tiff", height=2.5, width=6.5, units="in", res = 300, pointsize="10")
p1
dev.off()

我的问题在于:dat$col

【问题讨论】:

    标签: r colors ggplot2 percentile


    【解决方案1】:

    是的,你是对的,with(dat, factor(ifelse(fet &lt; 1.30102999566398, 3, quant))) 给出了一个“意外”的结果。 ifelse 中的 no 返回值 factor quant 被强制转换为与 yes 返回值 (3) numeric 相同的类。看看tail(dat[order(dat$fet), c("fet", "quant", "col")])

    #          fet quant col
    # 6   1.202582     0   3
    # 40  1.318997     0   1
    # 74  1.324552     0   1
    # 24  1.415189     1   2
    # 38  1.418230     0   1
    # 123 1.531584     0   1 
    

    对于 fet > 1.301(ifelse 中的 test),'col' 变为 1、1、2、1、1,而不是 0、0、1、0、0。发生了这样的事情:

    # original factor version of quant
    quant <- as.factor(0:2)
    quant
    # [1] 0 1 2
    # Levels: 0 1 2
    
    # coerce quant to numeric
    as.numeric(quant)
    # [1] 1 2 3
    

    比较这两个:

    set.seed(1)
    df <- data.frame(fet = rnorm(9), quant = factor(0:2))
    str(df)
    df$col <- with(df, ifelse(fet < 0, 3, quant))
    df
    
    set.seed(1)
    df <- data.frame(fet = rnorm(9), quant = 0:2)
    str(df)
    df$col <- with(df, ifelse(fet < 0, 3, quant))
    df
    

    因此,请尝试从您创建“量化”的ifelse 调用中删除factor,看看它是否解决了问题。

    另请参阅此处的 8.2.1:http://www.burns-stat.com/pages/Tutor/R_inferno.pdf‎。

    PS。当您说出您的问题时,单个 ifelse 行是您的实际问题(不是绘图部分)。如果是这样,您可能希望隔离此问题并压缩您的问题。

    【讨论】:

    • 这非常有用,但它的行为仍然无法预测。教学职责、实验室工作和奖学金应用程序让我有一段时间无法使用代码,但我会在本周再次查看它并编写更新并尝试深入了解它。我似乎只能在真实数据上一次使用一种配色方案或另一种配色方案。
    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 2014-11-26
    • 2020-07-02
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    相关资源
    最近更新 更多