本文介绍了scale_gradientn中的陡峭梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 使用 R 中的 ggplot2 ,我可以获得如下所示的离散色标: 这可以生成,如 显然这还不够好,因为可以在4个级别之间看到明显的色彩偏移 在ggplot2中完全有可能吗?解决方案使用离散量表: library(ggplot2) faithfuld $ classs< ;-cut(忠实的$密度,c(-Inf,0.01,0.02,0.03,Inf)) ggplot(忠实的,aes(等待,爆发))+ geom_raster(aes(fill = classes) )+ scale_fill_manual(name = density, values = c( red, green, blue, yellow),标签= c(0.01,0.02 ,0.03,))+ 指南(fill = guide_legend(label.vjust = -0.2)) Using ggplot2 in R, I can obtain a discrete color scale like the following:This can be generated as seen here.However, it does not look great. I'd like ro remove the spacing between the levels, and I thought that maybe I could switch to a continuous color scale, using scale_gradientn() and having a very steep gradient between different colors. This way I could use a continuous color scale, which has labels in the right places and looks great, instead of a discrete one.However, this is the best I could come up with:library(ggplot2)ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) + scale_fill_gradientn( colours = c("red", "green", "blue", "yellow"), values=c(0, 0.25, 0.25001, 0.5, 0.5001, 0.75, 0.75001,1) )Which clearly is not good enough, as significant color shifting can be seen between the 4 levels.Is this possible at all in ggplot2? 解决方案 Just use a discrete scale:library(ggplot2)faithfuld$classes <- cut(faithfuld$density, c(-Inf, 0.01, 0.02, 0.03, Inf))ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = classes)) + scale_fill_manual(name = "density", values = c("red", "green", "blue", "yellow"), labels = c(0.01, 0.02, 0.03, "")) + guides(fill = guide_legend(label.vjust = -0.2)) 这篇关于scale_gradientn中的陡峭梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 19:15