本文介绍了在geom_tile的colorbar指南中调整大小/手动输入中断,并替换y轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我重新回顾了大约一年前我遇到的这个问题。我希望我的'色彩'指南能够有效地以对数形式显示,以便在观看色彩时可以看到更深的蓝色值反映出更大的意义。 使用以下代码,我生成下面的图像: pz geom_tile(aes(fill = value))+ xlab(NULL)+ ylab(NULL)+ scale_fill_gradientn(colors = c(#000066,#0000FF,#DDDDDD,white), values = c(0,0.05,0.050000000000001,1.0), break = 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)) 或者,我可以将它显示为传说,而不是colourba r: 但我真正想要的是这样的: 我尝试添加'trans =log'(scale_fill_gradientn(trans =日志)),但是我的数据中有很多零,导致一个问题。 上一个措辞: $ b如果您有任何想法,我们将不胜感激! $ b 我正在尝试为各种分类的不同样本制作p值热图。我想在这个图上修改两件事: 我想调整我的 geom_tile 绘图强调图例比例的下端,同时仍然保持梯度的全部范围 - 类似于它看起来如果是对数刻度。所以基本上从1.0-0.05的白色到蓝色的转变以及从0.05-0.00的蓝色到深蓝色的转变将大致相等。有没有办法可以手动调整彩条指南? 我想替换y轴名称,以便删除我的空行标签。请注意,这些类别只是用字母表示,但在我的真实数据集中它们是长名称。我插入了虚拟数据行以将分类分割为卡盘,并将每个块内的切片从最重要的切换到不显着 - 我确信有更好的解决方案,但这是我在创建后我在堆栈溢出中发现的其他想法失败了很多次!我已经尝试用 scale_y_discrete 标记它们,但是这会因上述顺序而变得混乱。 帮助解决这些问题将非常感谢! code> 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 (1L,1L,1L,1L,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.40214650772221 e-06,0.0075828339,0.182 5924627, 0.0384381317,0.0440256659,0.3659284985,0.9777569144,1, 0.0075828339,1,0.2193606406,0.3659284985,0.0004289756,0 0.0011541045,0.0004289756,0.44400885491,0.6121402215,0.66724032426, 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) 以下是我的代码: col_blue ggplot(dat.m,aes(x = variable,y = Category))+ geom_tile(aes(fill = value))+ xlab(NULL)+ ylab(NULL)+ scale_fill_gradientn(colors = col_blue,values = c(1,0.05,0.01,0) ,guide =colorbar)+ theme_mary(base_size = 12) U PDATE: 所以现在我已经修改了代码,结果如下。我越来越接近我希望达到的目标,但我想用颜色比例来显示从0.05-0.0的梯度更清楚一点。 col_blue ggplot(dat.m,aes(x = variable ,y = Category))+ geom_tile(aes(fill = value))+ xlab(NULL)+ ylab(NULL)+ scale_fill_gradientn(colors = 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),colors = c(#FFFFFF,#000099,#000066,#000033)) scale_fill_gradientn 不要显示一个指南,其中 guide = FALSE code>,然后手动添加我们自己的限制设置为 c(0,0.1)(或任何你想要的范围)。 ggplot(da tm,aes(x = variable,y = Category))+ geom_tile(aes(fill = value))+ xlab(NULL)+ ylab(NULL)+ scale_fill_gradientn(colors = col_blue,values = c(1,0.05,0.01,0),guide = FALSE)+ scale_colour_gradientn(guide =colorbar,limits = c(0,0.1),colors = col_blue) 至于你的第二点,为什么不在绘图之前从源数据中删除空行? 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. 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. There are two things I would like to modify on this plot: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. 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. Is there a way that I can manually adjust the colorbar guide? I would like to replace the y-axis names so that I can remove my "empty" row label. 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.Help with either of these issues will be much appreciated!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.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")) 解决方案 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).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? 这篇关于在geom_tile的colorbar指南中调整大小/手动输入中断,并替换y轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 23:01