根据公式为ggplot背景着色

时间:2017-09-07 21:39:38

标签: r ggplot2

我想制作一个带有彩色背景的ggplot散点图,其中每个点的颜色由公式color = x * y决定。除此之外,我会绘制一堆积分。

背景的目的是让读者快速识别哪些点是等同的"因为x * y大致是相同的值。我想这可以用geom_raster和/或stat_function完成,但我不能弄清楚如何将函数串在一起。任何见解/提示都会有用,我会发布最终解决方案。

这里有一些骨架代码,所以你不必写一个例子。

library("ggplot2")

NRPercent <- function(x) {
    paste0(sapply(x * 100, scales::comma), "%")
}

data = data.frame(  count = c(  5e6,5e6,1e6,1e6, ## lots of experiments
                                    5e6,5e6,5e6, #RS22

                                    5e6,5e6,5e6,5e6,5e6, #RS30
                                    5e6,5e6,5e6,5e6, #RS30
                                    5e6,5e6,5e6,5e6,5e6, #RS30
                                    5e6,5e6,5e6,5e6,5e6, #RS30
                                    5e6,5e6,5e6,5e6, #RS30

                                    1e6,1e6,1e6,1e6,1e6, #RS31
                                    5e5,5e5,5e5,5e5,5e5, #RS31
                                    1e5,1e5,1e5,1e5,1e5, #RS31
                                    5e4,5e4,5e4,5e4,5e4 #RS31
                                    ),
                    percent = c(    1,1,1,1,
                                    0.13,0.475,0.83, 

                                    0.1,0.1,0.1,0.1,0.1,  #RS30
                                    0.01,0.01,0.01,0.01, #RS30
                                    0.001,0.001,0.001,0.001,0.001, #RS30
                                    0.0001,0.0001,0.0001,0.0001,0.0001, #RS30
                                    0.00001,0.00001,0.00001,0.00001, #RS30


                                    0.01,0.01,0.01,0.01,0.01,
                                    0.01,0.01,0.01,0.01,0.01,
                                    0.01,0.01,0.01,0.01,0.01,
                                    0.01,0.01,0.01,0.01,0.01
                                    ),
                    label = c(  "On","On","On","On",
                                    "On","On","On",

                                    "Not On","On","On","On","On",
                                    "Not On","On","On","On",
                                    "Not On","Not On","Not On","Not On","Not On",
                                    "Not On","Not On","Not On","Not On","Not On",
                                    "Not On","Not On","Not On","Not On",

                                    "Unknown","Unknown","Unknown","Unknown","Unknown",
                                    "Unknown","Unknown","Unknown","Unknown","Unknown",
                                    "Unknown","Unknown","Unknown","Unknown","Unknown",
                                    "Unknown","Unknown","Unknown","Unknown","Unknown"
                                ))

g = ggplot(data, aes(x=percent, y=count,color=label)) +
    geom_jitter(shape=16,width=0.2, height=0.1) +
    scale_y_continuous(trans='log1p',limits=c(40000,10000000),breaks=c(10e6,5e6,1e6,5e5,1e5,5e4,1e4)) +
    scale_x_continuous(trans='log',labels = NRPercent, expand=c(0,0), breaks=c(0,0.00001,0.0001,0.001,0.01,0.1,0.5)) +
    xlab("Percent")+
    ylab("Number") +
    theme_bw()


pdf("example_percent_vs_number.pdf")
print(g)
dev.off()

1 个答案:

答案 0 :(得分:1)

您可以尝试这样W2 = (W1 - 5 + 2*2)/1 + 1 = W1 - 1 + 1 = W1。我用geom_raster来填充

log10(color*percent)

ggplot(data, aes(x=percent, y=count,color=label)) + geom_jitter(shape=16,width=0.2, height=0.1) + geom_raster(aes(fill=log10(count*percent))) + scale_y_continuous(trans='log1p',limits=c(40000,10000000),breaks=c(10e6,5e6,1e6,5e5,1e5,5e4,1e4)) + scale_x_continuous(trans='log',labels = NRPercent, expand=c(0,0), breaks=c(0,0.00001,0.0001,0.001,0.01,0.1,0.5)) + xlab("Percent")+ ylab("Number") + theme_bw()

geom_tile

您需要根据自己的喜好调整宽度,高度和色阶(我会这样做,但你使用的是滑稽的轴)。请参阅下面的示例,以显示在正常轴上调整尺寸是微不足道的

ggplot(data, aes(x=percent, y=count,color=label)) +
    geom_jitter(shape=16,width=0.2, height=0.1) +
    geom_tile(aes(fill=log10(count*percent), x=percent, y=count)) +
    scale_y_continuous(trans='log1p',limits=c(40000,10000000),breaks=c(10e6,5e6,1e6,5e5,1e5,5e4,1e4)) +
    scale_x_continuous(trans='log',labels = NRPercent, expand=c(0,0), breaks=c(0,0.00001,0.0001,0.001,0.01,0.1,0.5)) +
    xlab("Percent")+
    ylab("Number") +
    theme_bw()

如何填写背景

从概念上讲,您需要使用值

填充绘图中的每个点
ggplot(mtcars, aes(x=cyl,y=mpg)) + 
  geom_tile(aes(fill=cyl*mpg, x=cyl, y=mpg, width=0.5, height=1)) + 
  geom_point()

再次这对你的情节来说很困难,因为它跨越了数量级,但上面应该让你开始

此外,您需要将背景密度值与实际数据点合并为单个data.frame以绘制两者。