本文介绍了如何使用ggplot2和geom_tile创建自定义色阶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想修改颜色渐变以匹配一组预定义的阈值/切点和颜色。 截止值:-0.103200,0.007022,0.094090,0.548600 颜色:#EDF8E9,#BAE4B3,# 74C476,#238B45 #创建示例数据 pp x df df $ r df $ z df pp(20) - >数据 #创建绘图 library(ggplo2) p p + geom_tile(aes(fill = z)) #Generate custom color ramp library(RColorBrewer) cols< - brewer.pal(4,Greens) 解决方案您可以尝试 scale_fill_brewer 。首先,你的z值: df df $ z_bin Plot: ggplot(data = df,aes(x = x,y = y,fill = z_bin))+ geom_tile()+ scale_fill_brewer(palette =Greens) I would like to modify the colour gradient in order to match a set of predefined thresholds/cutpoints and colours. How can I do this?Cutoff values: -0.103200, 0.007022, 0.094090, 0.548600Colors: "#EDF8E9", "#BAE4B3", "#74C476", "#238B45" #Create sample data pp <- function (n,r=4) { x <- seq(-r*pi, r*pi, len=n) df <- expand.grid(x=x, y=x) df$r <- sqrt(df$x^2 + df$y^2) df$z <- cos(df$r^2)*exp(-df$r/6) df } pp(20)->data#create the plotlibrary(ggplo2) p <- ggplot(pp(20), aes(x=x,y=y)) p + geom_tile(aes(fill=z))#Generate custom colour ramplibrary(RColorBrewer)cols <- brewer.pal(4, "Greens") 解决方案 You may try scale_fill_brewer. First, bin your z values:df <- pp(20)df$z_bin <- cut(df$z, breaks = c(-Inf, -0.103200, 0.007022, 0.094090, 0.548600))Plot: ggplot(data = df, aes(x = x, y = y, fill = z_bin)) + geom_tile() + scale_fill_brewer(palette = "Greens") 这篇关于如何使用ggplot2和geom_tile创建自定义色阶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 03:56