简体   繁体   English

调整大小/手动在geom_tile的颜色栏向导上输入中断,并替换y轴标签

[英]Resize/manually enter breaks on colorbar guide of geom_tile AND replace y-axis labels

I am revisiting this issue I ran into approximately a year ago. 我正在回顾大约一年前遇到的这个问题。 I would like my 'colourbar' guide to effectively be displayed on a log scale so that the takeaway when looking at it is that increasingly darker values of blue reflect greater significance. 我希望我的“色标”指南能够有效地以对数刻度显示,因此,从中可以看出,越来越深的蓝色值反映出更大的意义。

With the following code, I generate the below image: 使用以下代码,我生成以下图像:

pz <- ggplot(dat.m, aes(x=variable,y=Category)) +
  geom_tile(aes(fill=value)) +
  xlab(NULL) + ylab(NULL) +
  scale_fill_gradientn(colours=c("#000066","#0000FF","#DDDDDD","white"), 
                   values=c(0,0.05,0.050000000000001,1.0),
                   breaks=c(0, 0.000001, 0.01, 0.05, 1),
                   guide = "colourbar") +
  theme_bw()+
  theme(panel.background = element_blank(),
        panel.border = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank()) +
  theme(legend.position="top",
        legend.text = element_text(angle=45),
        axis.text.x = element_text(angle=45)
  )

彩条

Or, I can display it as a "legend" as opposed to a "colourbar": 或者,我可以将其显示为“传奇”而不是“颜色条”:

在此处输入图片说明

But what I really desire is something like this: 但是我真正想要的是这样的:

在此处输入图片说明

I have tried adding 'trans="log"' (scale_fill_gradientn(trans="log")), but there are lots of zeros in my data which causes a problem. 我尝试添加'trans =“ log”'(scale_fill_gradientn(trans =“ log”)),但是我的数据中有很多零,这会引起问题。 If you have any ideas it would be greatly appreciated! 如果您有任何想法,将不胜感激!


Previous wording: 以前的措辞:

I am trying to make a heatmap of p-values for different samples for various categorizations. 我正在尝试为各种分类的不同样本制作p值的热图。 There are two things I would like to modify on this plot: 我想在此情节上修改两件事:

  1. I would like to adjust the legend of my geom_tile plot to emphasize the lower end of the legend scale while still maintaining the full spectrum of the gradient - similar to how it would look if it were a log scale. 我想调整我的geom_tile图的图例以强调图例比例尺的下端,同时仍保持渐变的整个光谱-类似于如果是对数比例时的外观。 So essentially the white to blue transition from 1.0-0.05 and the blue to darkblue transition from 0.05-0.00 will be approximately equal in size. 因此,基本上从1.0-0.05的白色到蓝色的过渡和从0.05-0.00的蓝色到深蓝色的过渡在尺寸上大致相等。 Is there a way that I can manually adjust the colorbar guide? 有什么方法可以手动调整颜色栏指南?

  2. I would like to replace the y-axis names so that I can remove my "empty" row label. 我想替换y轴名称,以便删除“空”行标签。 Note, the Categories are simply represented as letters here, but in my real data set they are long names. 请注意,类别在这里只是用字母表示,但是在我的真实数据集中,它们是长名。 I have inserted "dummy" rows of data to split categorizations into chucks and ordered the tiles within each block to go from most significant to not significant - I am sure there is a better solution to this, but this is what I came up with after many failed attempts of other ideas I found on stack overflow! 我插入了“虚拟”数据行,以将分类划分为小块,并命令每个块中的磁贴从最高有效变为无效-我确定对此有更好的解决方案,但这是我后来想到的我在堆栈上发现的其他想法的许多尝试失败! I have tried labeling them with scale_y_discrete , but this gets jumbled with the aforementioned ordering. 我尝试用scale_y_discrete标记它们,但这与前面提到的顺序混杂在一起。

Help with either of these issues will be much appreciated! 对于这些问题中的任何一个的帮助将不胜感激!

sample_heatmap

Here is a sample dataset: 这是一个示例数据集:

dput(dat.m)
structure(list(Category = structure(c(12L, 11L, 10L, 9L, 8L, 
7L, 6L, 5L, 4L, 3L, 2L, 1L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 
4L, 3L, 2L, 1L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 
1L), class = "factor", .Label = c("j", "i", "empty2", "h", "empty1", 
"g", "f", "e", "d", "c", "b", "a")), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("b2", "c1", "c2"), class = "factor"), 
    value = c(7.40214650772221e-06, 0.0075828339, 0.1825924627, 
    0.0384381317, 0.0440256659, 0.3659284985, 0.9777569144, 1, 
    0.0075828339, 1, 0.2193606406, 0.3659284985, 0.0004289756, 
    0.0011541045, 0.0004289756, 0.4400885491, 0.6121402215, 0.6724032426, 
    0.2735924085, 1, 0.018824582, 1, 0.4386503891, 0.4249526456, 
    1.05094571578633e-05, 0.0027216795, 0.715979827, 0.0050376405, 
    0.7473334763, 0.9053300832, 1, 1, 0.0015392848, 1, 0.039679469, 
    0.0950327519)), .Names = c("Category", "variable", "value"
), row.names = c(NA, -36L), class = "data.frame")

And here is my code: 这是我的代码:

col_blue <- c("#FFFFFF","#000099","#000066","#000033")
ggplot(dat.m, aes(x=variable,y=Category)) +
  geom_tile(aes(fill=value)) +
  xlab(NULL) + ylab(NULL) +
  scale_fill_gradientn(colours=col_blue, values=c(1,0.05,0.01,0),guide="colorbar") +
  theme_mary(base_size=12)

UPDATE: 更新:

So now I have modified the code as such with the following results. 所以现在我修改了代码,结果如下。 I am getting closer to what I hope to achieve but I would like to play with the proportions of the colourbar to show the gradient from 0.05-0.0 a bit more clearly. 我离我希望达到的目标越来越近,但我想尝试使用颜色条的比例,以更清楚地显示从0.05-0.0的渐变。

col_blue <- c("#FFFFFF","#000099","#000066","#000033")
ggplot(dat.m, aes(x=variable,y=Category)) +
  geom_tile(aes(fill=value)) +
  xlab(NULL) + ylab(NULL) +
  scale_fill_gradientn(colours=col_blue, values=c(1,0.05,0.01,0), guide=FALSE) +
  scale_colour_gradientn(guide = "colourbar", limits = c(0,1),breaks=c(1,0.05,0.01,0),values=c(1,0.05,0.01,0),colours=c("#FFFFFF","#000099","#000066","#000033"))

image2

We can tell scale_fill_gradientn not to display a guide with guide=FALSE , then manually add our own with limits set to c(0,0.1) (or whatever range you want). 我们可以告诉scale_fill_gradientn不要显示带有guide=FALSE ,然后手动添加我们自己的guide=FALSE ,并将其限制设置为c(0,0.1) (或您想要的任何范围)。

ggplot(dat.m, aes(x=variable,y=Category)) +
  geom_tile(aes(fill=value)) +
  xlab(NULL) + 
  ylab(NULL) + 
  scale_fill_gradientn(colours=col_blue, values=c(1,0.05,0.01,0), guide=FALSE) +
  scale_colour_gradientn(guide = "colorbar", limits = c(0,0.1), colours=col_blue) 

在此处输入图片说明

As for your second point, why not just remove the "empty" rows from the source data before plotting? 关于第二点,为什么不在绘制之前就从源数据中删除“空”行?

for (1) , simply modify the data being used to drop the empty rows before (or as you are) plotting. 对于(1) ,只需在绘制之前(或您正在绘制(1)修改用于删除空行的数据。 eg: ggplot(dat.m[!grepl("^empty", dat.m$Category), ], aes(<etc>...)) 例如: ggplot(dat.m[!grepl("^empty", dat.m$Category), ], aes(<etc>...))

for (2) , you can override the aesthetics specifically just for the legend. 对于(2) ,您可以专门针对图例忽略美学。 Here is one example, adjust to your taste: + guides(fill=guide_legend(override.aes=list(alpha=1))) 这是一个示例,请根据自己的喜好进行调整: + guides(fill=guide_legend(override.aes=list(alpha=1)))

在此处输入图片说明

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

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