R ggplot2用于渐变图的离散调色板 [英] R ggplot2 discrete colour palette for gradient map

查看:132
本文介绍了R ggplot2用于渐变图的离散调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO用户的帮助下,我现在能够使用geom_tilescale_fill_gradient绘制渐变图.现在,我需要开一个固定的调色板.我的第一次尝试是使用scale_fill_manual,但收到此错误消息:

With the help from SO users I'm now able to plot a gradient map with geom_tile and scale_fill_gradient. Now I need to prescribe a fixed color palette. My first attempt was to use scale_fill_manual but I get this error message:

错误:连续值提供给离散刻度

Error: Continuous value supplied to discrete scale

这是原始剧情的代码

ggplot() + 
  geom_tile(data = idw.output, alpha = 0.8, aes(x = longitud, y = latitud, fill = RGlobal)) +
  scale_fill_gradient(low = "cyan", high = "orange",name = "UVI") +
  coord_map(xlim = c(-1.7, -1),ylim = c(37.7,38)) +
  ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")

所需的调色板是

paleta <- c("#4eb400","#a0ce00","#f7e400","#f8b600","#f88700","#f85900","#e82c0e","#d8001d","#ff0099","#b54cff","#998cff")

也许我需要重新格式化数据以产生一个新的合并数据列(在某些SO帖子中可以看到),该列可以与scale_fill_manual一起使用,但我无法成功

Maybe I need to reformat data to produce a new binned data column (seen in some SO posts) that can be used with scale_fill_manual but I couldn't succeed

这是用于图中最小示例的数据.

This are the data used for the minimal example in the plot.

dput(idw.output)
structure(list(longitud = c(-2, -1.9, -1.8, -1.7, -1.6, -1.5,
-1.4, -1.3, -1.2, -1.1, -1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4,
-0.3, -0.2, -0.0999999999999999, 0, -2, -1.9, -1.8, -1.7, -1.6,
-1.5, -1.4, -1.3, -1.2, -1.1, -1, -0.9, -0.8, -0.7, -0.6, -0.5,
-0.4, -0.3, -0.2, -0.0999999999999999, 0, -2, -1.9, -1.8, -1.7,
-1.6, -1.5, -1.4, -1.3, -1.2, -1.1, -1, -0.9, -0.8, -0.7, -0.6,
-0.5, -0.4, -0.3, -0.2, -0.0999999999999999, 0, -2, -1.9, -1.8,
-1.7, -1.6, -1.5, -1.4, -1.3, -1.2, -1.1, -1, -0.9, -0.8, -0.7,
-0.6, -0.5, -0.4, -0.3, -0.2, -0.0999999999999999, 0), latitud = c(37.7,
37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7,
37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.7, 37.8, 37.8,
37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8,
37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.8, 37.9, 37.9, 37.9,
37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9,
37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 38, 38, 38, 38, 38,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38
), RGlobal = c(2.47603407457515, 2.56722902275175, 2.65722090029236,
2.74600970719686, 2.83359544346532, 2.91997810909774, 3.0051577040941,
3.0891342284544, 3.17190768217865, 3.25347806526685, 3.333845377719,
3.4130096195351, 3.49097079071514, 3.56772889125912, 3.64328392116706,
3.71763588043894, 3.79078476907477, 3.86273058707454, 3.93347333443827,
4.00301301116594, 4.07134961725755, 3.02937209763967, 3.08883053029498,
3.14708589231421, 3.20413818369741, 3.25998740444456, 3.31463355455564,
3.36807663403068, 3.42031664286968, 3.47135358107261, 3.52118744863947,
3.56981824557031, 3.61724597186509, 3.6634706275238, 3.70849221254646,
3.75231072693307, 3.79492617068364, 3.83633854379815, 3.8765478462766,
3.915554078119, 3.95335723932535, 3.98995732989565, 3.54364142893506,
3.57136334606903, 3.59788219256697, 3.62319796842884, 3.64731067365467,
3.67022030824443, 3.69192687219814, 3.71243036551582, 3.73173078819742,
3.74982814024299, 3.76672242165248, 3.78241363242593, 3.79690177256333,
3.81018684206467, 3.82226884092997, 3.83314776915921, 3.84282362675239,
3.85129641370953, 3.85856613003061, 3.86463277571564, 3.86949635076461,
4.01884206846156, 4.01482747007424, 4.00960980105084, 4.00318906139137,
3.99556525109589, 3.98673837016434, 3.97670841859673, 3.96547539639308,
3.95303930355338, 3.93940014007761, 3.9245579059658, 3.90851260121793,
3.891264225834, 3.87281277981403, 3.853158263158, 3.83230067586593,
3.81024001793779, 3.78697628937361, 3.76250949017337, 3.73683962033708,
3.70996667986473), var1.var = c(1.01843285546171, 0.862794784692161,
0.732770319338128, 0.625346340387164, 0.537698048758539, 0.467188965298011,
0.411370930784953, 0.367984105924389, 0.334956971355076, 0.310406327642763,
0.292637295283383, 0.280143314704528, 0.271606146260796, 0.265895870237954,
0.262070886851961, 0.259377916247975, 0.257251998500358, 0.255316493614272,
0.253383081523816, 0.251451762093071, 0.249710855116251, 0.873286128828167,
0.727373752540586, 0.606635566366428, 0.508058451395788, 0.428817608648967,
0.366276559075455, 0.317987143554231, 0.281689522894669, 0.25531217783545,
0.236971909045211, 0.22497383712203, 0.217811402594311, 0.214166365919716,
0.212908807485795, 0.213097127609912, 0.213978046539185, 0.214986604450304,
0.215746161449985, 0.216068397574453, 0.215953312789854, 0.215589226991921,
0.887450912328089, 0.741743618032266, 0.621122629179308, 0.522574826962587,
0.443275412504286, 0.380587906854426, 0.332064150996824, 0.295444305840244,
0.268656852226255, 0.249818590925231, 0.237234642637044, 0.229398447992332,
0.224991767550493, 0.222884681801266, 0.222135591163608, 0.221991215986408,
0.221886596548835, 0.221445093059246, 0.220478385655308, 0.2189864744063,
0.217157679308732, 0.913284629008054, 0.764062048323634, 0.640189419045726,
0.538653622469394, 0.456629859817726, 0.391481652243769, 0.340760840830826,
0.302207586592512, 0.273750370470586, 0.253505993338174, 0.239779575997525,
0.231064559179595, 0.226042703546666, 0.223584089690392, 0.222747118131327,
0.222778509320894, 0.223113303639578, 0.223374861397818, 0.223374862835642,
0.223113308123219, 0.222778517360005)), .Names = c("longitud",
"latitud", "RGlobal", "var1.var"), row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26",
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70",
"71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
"82", "83", "84"), class = "data.frame")

预先感谢您的帮助

推荐答案

选项1:scale_fill_gradientn

ggplot() + 
  geom_tile(data = idw.output, alpha = 0.8, aes(x = longitud, y = latitud, fill = RGlobal)) +
  scale_fill_gradientn(colours = paleta) +
  coord_map(xlim = c(-1.7, -1), ylim = c(37.7,38)) +
  ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")

先转换为因子:

idw.output$RGlobal_f <- cut(idw.output$RGlobal, breaks = length(paleta))
ggplot() + geom_tile(data = idw.output, alpha = 0.8, aes(x = longitud, y = latitud, fill = RGlobal_f)) +
  scale_fill_manual(values = paleta) +
  coord_map(xlim = c(-1.7, -1),ylim = c(37.7,38)) +
  ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")

有关如何提供其他标签的信息,请参见?cut.

See ?cut on how to provide other labels.

我绝对更喜欢选项1!

这篇关于R ggplot2用于渐变图的离散调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆