簡體   English   中英

R ggplot2用於漸變映射的離散調色板

[英]R ggplot2 discrete colour palette for gradient map

在SO用戶的幫助下,我現在能夠使用geom_tilescale_fill_gradient繪制漸變映射。 現在我需要開一個固定的調色板。 我的第一次嘗試是使用scale_fill_manual但我收到此錯誤消息:

錯誤:提供給離散比例的連續值

這是原始圖的代碼

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一起使用,但我無法成功

這是用於圖中最小示例的數據。

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(" ")

在此輸入圖像描述

選項2: scale_fill_manual

首先轉換為因子:

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如何提供其他標簽。

在此輸入圖像描述

我絕對更喜歡選項1!

將我的評論轉換為答案:

# create a factor variable
idw.output$RGlobal2 <- cut(idw.output$RGlobal, 11)

# plot
ggplot(idw.output, aes(x = factor(longitud), y = factor(latitud), fill = RGlobal2)) + 
  geom_tile() +
  scale_x_discrete(expand = c(0,0)) +
  scale_y_discrete(expand = c(0,0)) +
  scale_fill_manual(values = paleta) +
  ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")

贈送:

在此輸入圖像描述

通過將aes內的xy變量轉換為因子,可以確保軸標簽位於x和y軸上的切片中間。


在回復您的意見時,您可以按如下方式處理:

# cut 'RGlobal' in equal lengths from the minimum value to the maximum value
# by making use of the 'length.out' parameter of the 'cut' function
idw.output$Rfactor <- cut(idw.output$RGlobal, breaks = seq(2.4, 4.1, length.out = 11))
niveles <- levels(idw.output$Rfactor)

# create the plot
ggplot(idw.output, aes(x = factor(round(longitud,1)), y = factor(latitud), fill = Rfactor)) + 
  geom_tile() +
  scale_x_discrete(expand = c(0,0)) +
  scale_y_discrete(expand = c(0,0)) +
  scale_fill_manual(name = "UVI", values = paleta, breaks = niveles) +
  labs(title = "Previsión UVI", x = " ", y = " ")

# instead of 'labs(title = "Previsión UVI", x = " ", y = " ")'
# you can also use: 'labs(title = "Previsión UVI") + theme(axis.title = element_blank())'

這使:

在此輸入圖像描述


另一種選擇是使用scale_fill_gradientn作為@Axeman在他的答案中所做的,但是將x和y軸轉換為因子變量以更好地定位軸標簽:

ggplot(idw.output, aes(x = factor(round(longitud,1)), y = factor(latitud), fill = RGlobal)) + 
  geom_tile() +
  scale_x_discrete(expand = c(0,0)) +
  scale_y_discrete(expand = c(0,0)) +
  scale_fill_gradientn(colours = paleta) +
  labs(title = "Previsión UVI") +
  theme(axis.title = element_blank())

這使:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM